1

Warning: 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.

Code:

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

I am not sure where the problem is... it has the UICollectionViewFlowLayoutAutomaticSize and like a wrap_content on iPads. The error sounds like UICollectionViewFlowLayoutAutomaticSize; it works sometimes. When it does, works perfectly. When it doesn't, it freezes and fills the Log as follows:

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.

And basically, it repeats the same Log over and over again.

Thank you in advance.

Amg91
  • 165
  • 8
  • 25

1 Answers1

6

Your problem appears because the reference width is bigger then "working" width of your collectionView

To fix this you must calculate your cells reference size properly.

You're using UICollectionViewFlowLayout, so you can take an advantage of UICollectionViewDelegateFlowLayout and implement the following method (It is provided for the case when cells has fullscreen width and dynamic height):

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

Keep in mind, that if you're using multiple columns, you should calculate cells width accordingly, and minInteritemSpacing should be taken into account. Hope it helps.

fewlinesofcode
  • 3,007
  • 1
  • 13
  • 30