Amit Patil
Amit Patil

Reputation: 21

ios swift UICollectionVIew Issue: The behavior of the UICollectionViewFlowLayout is not defined because

I am using UICollectionView using the flow layout. I have made a custom UICollectionView with horizontal. error-

The behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

The relevant UICollectionViewFlowLayout instance is , and it is attached to ; layer = ; contentOffset: {-8, -8}; contentSize: {0, 100}> collection view layout: . 2019-11-18 11:51:59.017124+0530 eVyapaar_Grocery[3964:1811183] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

Here is my code:
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UINib(nibName: "cellID", bundle: nil), forCellWithReuseIdentifier:"cellID")
collectionView.contentInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
collectionView.showsHorizontalScrollIndicator = false
collectionView.isScrollEnabled = true
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
       layout.scrollDirection = .horizontal
       collectionView!.collectionViewLayout = layout
}

extension ViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if collectionView == myCcollectionView {
            var width: CGFloat = 0
            if UIDevice.isIpad {
                width = ((UIScreen.main.bounds.width/6)-6)
            } else {
                width = ((UIScreen.main.bounds.width/3)-6)
            }
            return CGSize(width: width, height: width)
        } 
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 1

    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
}

Upvotes: 1

Views: 3738

Answers (1)

Elhoej
Elhoej

Reputation: 751

As the error says, your cell size has to be smaller than your collectionView's height & width minus the content insets as to fit the cells inside the view. As I cant see the size of your collectionView with the code you have provided, I can't give you a definite answer, but try to reduce the size of your collectionView cells. Also note that you are setting the contentInsets 2 different places,

here:

collectionView.contentInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)

and here:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) }

edit: Try to give them this size:

extension ViewController: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { if collectionView == myCcollectionView { var width: CGFloat = 0 if UIDevice.isIpad { width = ((collectionView.frame.width/6)-8) } else { width = ((collectionView.frame.width/3)-8) } return CGSize(width: width, height: width) } }

Upvotes: 1

Related Questions