UICollectionView 在较小的设备上导致 "UICollectionViewFlowLayoutBreakForInvalidSizes"

UICollection View causes "UICollectionViewFlowLayoutBreakForInvalidSizes" on smaller devices

在 iPhone 6 Plus 上,集合视图单元格很好,但是在 iPhone 5 等其他设备尺寸上进行测试时,我被“

轰炸了

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.

尤其是当我这样做的时候:

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

无论如何,我可以解决这个问题吗?我需要确保单元可缩放并适合所有设备。

通过 class 确认 UICollectionViewDelegate。委托方法 sizeForItemAtIndexPath 将从 UICollectionViewDelegateFlowLayout 调用,这将调用您的 class 现在您可以 return UICollectionViewCell.

的大小

这对我有用

方法:

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
    }

这里CollectionView是UICollectionView的outlet

也有可能当您在 UITableViewCell 中有一个 collectionView 时,此单元格实际上将 collectionView 的高度调整为 0。

在我的示例中,collectionView 显示了一个 UIImageView 数组,以在每个单元格中显示一些图片库。 如果单元格不应该显示任何图像(例如没有图像的文本),则隐藏 collectionView

collectionView,如果放在 StackView 中可能需要有高度限制并且为了避免冲突,您可能需要将其设置为优先级 999。

然后,当您隐藏 collectionView 时,它实际上会尝试将高度设置为 0。 collectionView 仍然可以在其中包含图像,这将触发错误。

解决方案:清空你的 datasource 并在你配置单元格时重新加载 collectionView