1

警告: 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()
            }
        }
    }
}

我不确定问题出在哪里......它在 iPad 上有UICollectionViewFlowLayoutAutomaticSize和。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.

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

先感谢您。

4

1 回答 1

6

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

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

您正在使用UICollectionViewFlowLayout,因此您可以利用UICollectionViewDelegateFlowLayout并实现以下方法(它是为单元格具有全屏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应予以考虑。希望能帮助到你。

于 2018-11-20T12:09:33.907 回答