UICollectionViewFlowLayout not invalidating right on orientation change

ios, uicollectionview, uicollectionviewlayout

Solution

One solution is to use KVO and listen for a change to the frame of the superview for your collection view. Calling -reloadData will work and get rid of the warnings. There is an underlying timing issue here...

// -loadView
[self.view addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];

// -dealloc
[self.view removeObserver:self forKeyPath:@"frame"];

// KVO Method
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    [collectionView_ reloadData];
}

Problem

i have a UICollectionView with a UICollectionViewFlowLayout. I also implement the UICollectionViewDelegateFlowLayout protocol. In my datasource i have a bunch of UIViewControllers which respond to a custom protocol so i can ask for their size and some other stuff. In in the FlowLayout delegate when it asks for the sizeForItemAtIndexPath:, i return the item size which i get from my protocol. The ViewControllers which implement my protocol return a different item size depending on the orientation. Now if i change the device orientation from portrait to landscape theres no problem (Items are larger in landscape) but if i change it back, i get this warning: ``` the item width must be less that the width of the UICollectionView minus the section insets left and right values. Please check the values return by the delegate. ``` It still works but i don't like it to get warnings so maybe you can tell me what i am doing wrong. Also there is another problem. If i don't tell my collectionViews collectionViewLayout to invalidate in willAnimateRotationToInterfaceOrientation: the sizeForItemAtIndexPath: is never called. Hope you understand what i mean. If you need additional information let me know :)

Original source