UICollection View causes “UICollectionViewFlowLayoutBreakForInvalidSizes” on smaller devices

蓝咒 提交于 2019-12-23 13:03:58

问题


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.


回答1:


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)
}



回答2:


 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




回答3:


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.



来源:https://stackoverflow.com/questions/44559745/uicollection-view-causes-uicollectionviewflowlayoutbreakforinvalidsizes-on-sma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!