Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollection View causes "UICollectionViewFlowLayoutBreakForInvalidSizes" on smaller devices

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.

like image 569
Jevon Cowell Avatar asked Jun 15 '17 06:06

Jevon Cowell


Video Answer


2 Answers

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)
}
like image 199
Salman Ghumsani Avatar answered Nov 08 '22 15:11

Salman Ghumsani


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.

like image 38
Mikael Avatar answered Nov 08 '22 14:11

Mikael