The behavior of the UICollectionViewFlowLayout is not defined, because the cell width is greater than collectionView width

ios9uicollectionviewuicollectionviewcelluicollectionviewlayoutxcode7-beta5

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 <UICollectionViewFlowLayout:
0x7f24d670>, and it is attached to <UICollectionView: 0x7b377200;
frame = (0 0; 768 1024); clipsToBounds = YES; autoresize = W+H;
gestureRecognizers = <NSArray: 0x7f24ce30>; animations = {
position=<CABasicAnimation: 0x7f279d60>;
bounds.origin=<CABasicAnimation: 0x7f279430>;
bounds.size=<CABasicAnimation: 0x7f279480>; }; layer = <CALayer:
0x7aec9cf0>; contentOffset: {0, 0}; contentSize: {1024, 770}>
collection view layout: <UICollectionViewFlowLayout: 0x7f24d670>.
2015-08-18 16:07:51.524 Example[16070:269647] Make a symbolic
breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch
this in the debugger.

That is what I get and what I do

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(self.collectionView!.frame.size.width - 20, 66)
    }

I rotate from landscape to portrait then the console shows this error message only in iOS 9 if anyone knows what happens and if there is a fix for this?

Screenshot

Best Answer

This happens when your collection view resizes to something less wide (go from landscape to portrait mode, for example), and the cell becomes too large to fit.

Why is the cell becoming too large, as the collection view flow layout should be called and return a suitable size ?

collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath)

Update to include Swift 4

@objc override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{  ...  }

This is because this function is not called, or at least not straight away.

What happens is that your collection view flow layout subclass does not override the shouldInvalidateLayoutForBoundsChange function, which returns false by default.

When this method returns false, the collection view first tries to go with the current cell size, detects a problem (which logs the warning) and then calls the flow layout to resize the cell.

This means 2 things :

1 - The warning in itself is not harmful

2 - You can get rid of it by simply overriding the shouldInvalidateLayoutForBoundsChange function to return true. In that case, the flow layout will always be called when the collection view bounds change.

Related Topic