1

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) } } 
1
  • Unwanted padding getting added every time on top and bottom side when collection view reload. I am trying to solve this issue from the last 2 days but unable to solve it. anyone can help me? thanks in advance Commented Nov 18, 2019 at 9:21

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

hey thanks for the reply here is the line that I calculated my cell collectionView.reloadData() cHeightOfCollectionView.constant = collectionView.collectionViewLayout.collectionViewContentSize.height + 24
my collectionView height is 100 and I set value according to your code but it's not worked

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.