【问题标题】:UICollectionView warningUICollectionView 警告
【发布时间】:2018-10-04 16:38:41
【问题描述】:

警告: 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.

代码:

extension SaleViewController {
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator){
        checkEstimatedSize()
    }

    func checkEstimatedSize(){
        if(DeviceType.IS_IPAD || UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight){
            if let layout = myCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
                layout.estimatedItemSize = CGSize(width: 1, height: 1)
                layout.invalidateLayout()
            }
        }else{
            if let layout = myCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
                layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
                layout.invalidateLayout()
            }
        }
    }
}

我不确定问题出在哪里...它有 UICollectionViewFlowLayoutAutomaticSize 和 iPad 上的 wrap_content。 错误听起来像UICollectionViewFlowLayoutAutomaticSize;它有时会起作用。当它这样做时,完美地工作。如果没有,它会冻结并按如下方式填充日志:

The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x10313a9a0>, and it is attached to <UICollectionView: 0x104096e00; frame = (10 138; 300 420); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x1c0458750>; layer = <CALayer: 0x1c02254e0>; contentOffset: {0, 0}; contentSize: {300, 220}; adjustedContentInset: {0, 0, 0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x10313a9a0>. 2018-10-04 11:03:35.869371-0500 MyApp[276:5353] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger. 2018-10-04 11:03:35.869398-0500 MyApp[276:5353] The behavior of the UICollectionViewFlowLayout is not defined because: 2018-10-04 11:03:35.869445-0500 MyApp[276:5353] 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. 2018-10-04 11:03:35.869555-0500 MyApp[276:5353] Please check the values returned by the delegate.

基本上,它一遍又一遍地重复相同的日志。

提前谢谢你。

【问题讨论】:

  • 在模拟器上工作正常???那么iOS版本可能会有所不同吗?它适用于某些版本而其他版本不适用?
  • 运行大量测试后,似乎两者都有问题。更新问题。

标签: ios swift uicollectionview uicollectionviewlayout uicollectionviewflowlayout


【解决方案1】:

出现您的问题是因为引用 width 大于您的 collectionView 的“工作”宽度

要解决此问题,您必须正确计算单元格参考大小。

你使用的是UICollectionViewFlowLayout,所以你可以利用UICollectionViewDelegateFlowLayout,实现如下方法(针对cell全屏width和动态高度的情况提供):

        /// UICollectionViewDelegateFlowLayout
        func collectionView(_ collectionView: UICollectionView,
                            layout collectionViewLayout: UICollectionViewLayout,
                            sizeForItemAt indexPath: IndexPath) -> CGSize {
            var referenceHeight: CGFloat = 54.0 // Approximate height of the cell
            // Cell width calculation
            let sectionInset = (collectionViewLayout as! UICollectionViewFlowLayout).sectionInset
            let referenceWidth = collectionView.safeAreaLayoutGuide.layoutFrame.width
                - sectionInset.left
                - sectionInset.right
                - collectionView.contentInset.left
                - collectionView.contentInset.right

                return CGSize(width: referenceWidth, height: referenceHeight)
            }
        }

请记住,如果您使用多列,则应相应地计算单元格width,并应考虑minInteritemSpacing。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2015-07-15
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    相关资源
    最近更新 更多