UICollectionView does not display cells correctly after changing frames

I noticed that if I changed the UICollectionView frame (for example, when switching the status bar in a call), the collection view does not update its cells properly for its new frame. This is probably the easiest to see in a short video:

http://cl.ly/2t2Y2A3A2w1D/CollectionViewTest.mov

The source files for this simple test are:

http://cl.ly/0S0A360B3I3Q/CollectionViewTest.zip

It doesn't seem to matter if I'm using a UICollectionViewController or a UIViewController. Has anyone seen this? Am I missing something? The only workaround I found was to call reloadData in the collection view in the ViewWillLayoutSubviews or viewDidLayoutSubviews controller view, which works, but is far from ideal, when user drag and drop on the collection view frame because reloadData is called many times and leads to very slow user updates interface when dragging a user.

+7
source share
3 answers

I had a similar problem in which my UICollectionView frame changed, and if I don't call reloadData , it stuck in the same place without moving with CollectionView .

I fixed this by adding this to my CollectionViewFlowLayout:

 - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return YES; } 

It works well.

+9
source

I had a similar problem. I just needed to resize the collections frame so that the menu displayed below, and the user could still scroll far enough so that the menu did not close the cells at the bottom.

Changing the content insert worked for me.

 self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, rect.size.height+8, 0); 

I could not change the structure of the collection view to work without reloading the data in order to cross out cells that were already selected by the user.

+2
source

You can have your UICollectionViewController register for UIApplicationWillChangeStatusBarFrameNotification and reloadData in the selector. Thus, you will reload the data only when the status bar changes the frame.

 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callCameIn:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil]; } -(void)callCameIn:(NSNotification *) aNote { [self.collectionView reloadData]; } 
+1
source

All Articles