0
votes

I am trying to install a collectionView that has cells who scroll horizontally. I can see the cells but for some reason, the section doesn't scroll. I have no idea why the collectionView is not registering when I try to interact with it but the code I am using is below,

    let recentCollectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.backgroundColor = UIColor.clear
    collectionView.translatesAutoresizingMaskIntoConstraints = false
    
    return collectionView
}()

func setupViews() {
    
    backgroundColor = UIColor.clear
    
    addSubview(recentCollectionView)
    contentView.isUserInteractionEnabled = true
    recentCollectionView.dataSource = self
    recentCollectionView.delegate = self
    recentCollectionView.register(recentCell.self, forCellWithReuseIdentifier: cellID)
    
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": recentCollectionView]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": recentCollectionView]))
    
 
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return reviews.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! recentCell
    cell.configureCell(reviews[indexPath.row])
    
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
     return CGSize(width: 100, height: frame.height - 5)
 }


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

in my console, I am seeing the warning

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.

Why does this show? The collectionView cells height are not taller than the actual collectionView itself.

1
Note: withVisualFormat: "V:|[v0]|" is the old way of adding constraint... check out this article. - aheze
@aheze Thanks for this. I am still facing same issue after making these changes. - zach wilcox

1 Answers

1
votes

Let's say you cell is 1000pts tall, you are setting the collection view cell height to 995 and setting the collection view top inset to 100 which returns 900. So the problem is that your collectionView cell is 95pts taller than the collectionView.

You need to make your collectionView cell shorter or lower the collectionView top inset.