I am implementing a collection view whose elements are sized based on the boundaries of the collection view. Therefore, when the size changes, due to the rotation of the device, for example, I need to invalidate the layout so that the cell sizes are changed in order to consider the new boundaries of the collection view. I did this using the viewWillTransitionToSize API.
This works well until the user presents a modal view controller over the view controller that contains the collection view, rotates the device, and then rejects it. However, the item size is not updated to the appropriate size. viewWillTransitionToSize is invoked, and the layout is considered invalid, as expected, but collection view restrictions are still the old value. For example, when you turn from portrait to landscape, the value of the collection viewing restriction still has a height greater than the width. I'm not sure why this is so, but I wonder if this is the best way to invalidate when resizing? Is there a better way to do this?
I also tried to subclass UICollectionViewFlowLayout and override shouldInvalidateLayoutForBoundsChange to return YES , but for some reason this does not work even when rotating without a modal representation. It also does not use an appropriate viewing frame.
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(nonnull id<UIViewControllerTransitionCoordinator>)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; [self.collectionView.collectionViewLayout invalidateLayout]; [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> __nonnull context) { [self.collectionView.collectionViewLayout invalidateLayout]; } completion:^(id<UIViewControllerTransitionCoordinatorContext> __nonnull context) {
I also tried to include it in the completion block, but it still does not use the correct collection view frames.
If I invalidate the layout in viewWillAppear , it uses the correct collection view frames, which fix the rotation issue with the modally presented view controller. But this is not necessary, perhaps there are other situations where this will not be properly appreciated.
ios uicollectionview uicollectionviewlayout
Joey
source share