@@ -114,7 +114,8 @@ - (void)viewDidLoad
114114 [self .view addGestureRecognizer: self .longPressGestureRecognizer];
115115
116116 self.layout .sectionInsetReference = UICollectionViewFlowLayoutSectionInsetFromSafeArea;
117-
117+ [self registerForTraitObservation ];
118+
118119 [self refreshDataAnimated: NO ];
119120}
120121
@@ -317,18 +318,6 @@ - (void)viewDidDisappear:(BOOL)animated
317318 [self unregisterForKeyboardNotifications ];
318319}
319320
320- - (void )traitCollectionDidChange : (UITraitCollection *)previousTraitCollection
321- {
322- [super traitCollectionDidChange: previousTraitCollection];
323-
324- if ([self shouldShowCustomHeaderView ]) {
325- // If there's a custom header, we'll invalidate it so that it can adapt itself to dynamic type changes.
326- UICollectionViewFlowLayoutInvalidationContext *context = [UICollectionViewFlowLayoutInvalidationContext new ];
327- [context invalidateSupplementaryElementsOfKind: UICollectionElementKindSectionHeader atIndexPaths: @[ [NSIndexPath indexPathForRow: 0 inSection: 0 ] ]];
328- [self .collectionView.collectionViewLayout invalidateLayout ];
329- }
330- }
331-
332321- (UIViewController *)viewControllerToUseToPresent
333322{
334323 // viewControllerToUseToPresent defaults to self but could be set to nil. Reset to self if needed.
@@ -1466,6 +1455,20 @@ - (BOOL)isPresentedAsPopover
14661455 return NO ;
14671456}
14681457
1458+ - (void )registerForTraitObservation
1459+ {
1460+ __weak typeof (self) weakSelf = self;
1461+ [self registerForTraitChanges: @[UITraitPreferredContentSizeCategory.class]
1462+ withHandler: ^(id <UITraitEnvironment> traitEnvironment, UITraitCollection *previousCollection) {
1463+ if ([weakSelf shouldShowCustomHeaderView ]) {
1464+ // If there's a custom header, invalidate it so that it can adapt itself to dynamic type changes.
1465+ UICollectionViewFlowLayoutInvalidationContext *context = [UICollectionViewFlowLayoutInvalidationContext new ];
1466+ [context invalidateSupplementaryElementsOfKind: UICollectionElementKindSectionHeader atIndexPaths: @[ [NSIndexPath indexPathForRow: 0 inSection: 0 ] ]];
1467+ [weakSelf.collectionView.collectionViewLayout invalidateLayout ];
1468+ }
1469+ }];
1470+ }
1471+
14691472- (void )registerForKeyboardNotifications
14701473{
14711474 [NSNotificationCenter .defaultCenter addObserver: self selector: @selector (keyboardWillShowNotification: ) name: UIKeyboardWillShowNotification object: nil ];
0 commit comments