We are using a UICollectionView to display cell that cover the full screen (minus the status and nav bar). The cell size is set from self.collectionView.bounds.size
:
- (void)viewWillAppear:(BOOL)animated
{
//
// value isn't correct with the top bars until here
//
CGSize tmpSize = self.collectionView.bounds.size;
_currentCellSize = CGSizeMake( (tmpSize.width), (tmpSize.height));
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return _currentCellSize;
}
This sets the correct sizing for each device. Each cell is defined to have no insets, and the layout has no header or footer. However, when we rotate from portrait to landscape we get the following "complaint":
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less that the height of the UICollectionView minus the section insets top and bottom values.
Now I understand this error, however we reset the size of the cell and use the flow layouts built in rotation transition:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//self.collectionView.bounds are still the last size...not the new size here
}
- (void)didRotateFromInterfaceOrientation: UIInterfaceOrientation)fromInterfaceOrientation
{
CGSize tmpSize = self.collectionView.bounds.size;
_currentCellSize = CGSizeMake( (tmpSize.width), (tmpSize.height));
[self.collectionView performBatchUpdates:nil completion:nil];//this will force the redraw/size of the cells.
}
The cells render correctly in landscape.
It seems as though the Flow Layout sees the old cell size (which causes the complaint since it will be too tall), but does read/render the new cell size set in didRotateFromInterfaceOrientation
.
Is there a way to get rid of the complaint?
We've tried finding another hook during a device rotate transition that has access to the correct target screen size (vs the current screen size) with no luck. Debug output shows the complaint happens after willRotateToInterfaceOrientation
but before didRotateFromInterfaceOrientation
.
We've also verified the obvious; if we set up the cell height to be a fixed size less than the landscape screen height, the complaint doesn't occur. Also, the complaint does not occur when rotating from landscape back to portrait.
Everything runs fine, and renders correctly. However this complaint worries us. Anyone else have any ideas or solutions?
I was getting the same warning. Unsatisfied with the "reloadData" approach, I found that calling
[self.collectionView.collectionViewFlowLayout invalidateLayout]
before setting the frame of the collection view silenced the warning and yielded the expected results.Just encountered and fixed the same problem. Since my solution is more along the lines you were asking for and doesn't match any existing answer, I've posted it here.
This solved it for me:
And I am using this delegate method for setting the size:
And I can from there use this with the correct frame after rotation:
I've used an
UICollectionViewFlowLayoutInvalidationContext
, in which I calculate the new offset such that it maintains the same content offset. My own functioncollectionViewSizeForOrientation:
returns the proper size. Its not perfect, but at least it's not sketchy:collectionViewSizeForOrientation:
in my case is the following, assuming that insets and item spacing are 0:}
Not to throw another shrimp on this loaded, yet unaccepted, barbie.
Setting the content insets just before returning the cell size did the trick for me.
Note:
I am using a container view in a storyboard to load the collection view within a UIViewController. I tried setting this on the flowLayout object in the storyboard. The collection view in the storyboard. And overriding one of the UICollectionViewDelegateFlowLayout; though I do not remember which one. I'm also not sure if this will work for a vertical layout.
My fix was as simple as unchecking 'Adjust Scroll View Insets' for the view controller in IB, since I needed my navigation bar to be translucent.