2015-08-18 16:07:51.523 Example[16070:269647] the behavior of the UICollectionViewFlowLayout is not defined because: 2015-08-18 16:07:51.523
Example[16070:269647] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
2015-08-18 16:07:51.524 Example[16070:269647] The relevant UICollectionViewFlowLayout instance is , and it is attached to ; animations = { position=; bounds.origin=; bounds.size=; }; layer = ; contentOffset: {0, 0}; contentSize: {1024, 770}> collection view layout: . 2015-08-18 16:07:51.524 Example[16070:269647] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
This is what I get, what I do is
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(self.collectionView!.frame.size.width - 20, 66)
}
when I rotate from landscape to portrait, the console shows this error message only in iOS 9, does anyone know what this happens and if there is a fix for this?
I am using a subclass of
UICollectionViewFlowLayout
and using itsitemSize
property to specify the cell size (instead of thecollectionView:sizeForItemAtIndexPath:
delegate method). Every time I rotate the screen to a shorter width one (e.g. landscape -> portrait) I get this huge warning in question.I was able to fix it by doing 2 steps.
Step 1: In
UICollectionViewFlowLayout
subclass'sprepareLayout()
function, movesuper.prepareLayout()
to after whereself.itemSize
is set. I think this makes the super class to use the correctitemSize
value.Note that the above change will somehow make the layout size change when screen rotates stops working. This is where step 2 comes in.
Step 2: Put this in the view controller that holds the collection view.
Now the warning is gone :)
Some Notes:
Make sure you are adding constraints to the collectionView and not using
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
Make sure you are not calling
invalidateLayout
inviewWillTransitionToSize()
because the width of an edge-to-edge cell in landscape is larger than the collection view’s frame width in portrait. See below references.References
This is what you need:
You can check in debugger if
collectionView.contentOffset
is changed to negative in my case it changes from(0,0)
to(0,-40)
. You can solve this issue by using this methodI had a similar issue.
In my case, I had a collection view and when you tapped on one of the cells, a popover with a
UITextField
opened, to edit the item. After that popover disappeared, theself.collectionView.contentInset.bottom
was set to 55 (originally 0).To fix my issue, after the popover view disappears, I’m manually setting contentInset to
UIEdgeInsetsZero
.The original issue seems to be related to the contextual prediction bar that shows up on top of the keyboard. When the keyboard is hidden, the bar disappears, but the
contentInset.bottom
value is not restored to the original value.Since your issue seems to be related to the width and not to the height of the cell, check if any of the
contentInset
orlayout.sectionInset
values are the same as the one set by you.I've solve this problem by using
safeAreaLayoutGuide
.and you also have to override this function to support portrait and landscape mode correctly.