LaVOZs

The World’s Largest Online Community for Developers

'; ios - UICollection View causes "UICollectionViewFlowLayoutBreakForInvalidSizes" on smaller devices - LavOzs.Com

On an iPhone 6 Plus, the collection view cells are fine, but when testing on another device size like the iPhone 5, i'm bombarded with "

017-06-15 01:59:25.744 HyperTest[3865:8825385] The behavior of the UICollectionViewFlowLayout is not defined because: 2017-06-15 01:59:25.744 HyperTest[3865:8825385] 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. 2017-06-15 01:59:25.745 HyperTest[3865:8825385] The relevant UICollectionViewFlowLayout instance is , and it is attached to ; layer = ; contentOffset: {0, 0}; contentSize: {414, 219}> collection view layout: . 2017-06-15 01:59:25.745 HyperTest[3865:8825385] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

especially when I do this:

let layout = billFeedCollectionView.collectionViewLayout as? UICollectionViewFlowLayout // casting is required because UICollectionViewLayout doesn't offer header pin. It's feature of UICollectionViewFlowLayout
    layout?.sectionHeadersPinToVisibleBounds = true
    layout?.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize

anyway, I can resolve this problem? I need to ensure that the cell scales and fits all devices.

By confirming UICollectionViewDelegate from your class. The delegate method sizeForItemAtIndexPath will call from UICollectionViewDelegateFlowLayout and this will call in your class now you can return the size of UICollectionViewCell.

This works for me

method:

collectionView.delegate = self

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
     return CGSizeMake(collectionView.frame.size.width, collectionView.frame.size.height)
}
 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {

        let cellsize = CGSize(width: (CollectionView.bounds.size.width/2) - 12, height:(CollectionView.bounds.size.height/3) - 20)



        return cellsize
    }

here CollectionView is outlet of UICollectionView

It's also possible that when you have a collectionView in a UITableViewCell this cell actually resizes the collectionView height to 0.

In my case, the collectionView is showing an array of UIImageView to display some image gallery in each cell. If the cell is not supposed to show any images (text without image for example), the collectionView is hidden.

The collectionView, if put within a StackView might need to have a height constraint and to avoid conflicts, you might need to set it to priority 999.

Then, when you hide the collectionView, it will actually try to set the height to 0. The collectionView can still contain images in it and that will trigger the error.

SOLUTION: empty your datasource and reload the collectionView when you configure your cell.

Related
Passing Data between View Controllers
the behavior of the UICollectionViewFlowLayout is not defined, because the cell width is greater than collectionView width
UIDocumentMenuViewController crashes on iPad but not on iPhone
Why is UICollectionView insetForSectionsAtIndex not being called?
UICollectionViewFlowLayout exception when executing XHR-Request in NativeScript
UICollectionViewCell width greater than UICollectionView error handling
CollectionView AutoResize Label Not Working
UICollectionView full width and height problem
ios swift UICollectionVIew Issue: The behavior of the UICollectionViewFlowLayout is not defined because