UICollectionView 水平滚动无法正常工作

UICollectionView horizontal scroll not working properly

我正在尝试安装一个包含水平滚动单元格的 collectionView。我可以看到单元格,但出于某种原因,该部分不会滚动。我不知道为什么当我尝试与它交互时 collectionView 没有注册,但我使用的代码如下,

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

在我的控制台中,我看到警告

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.

为什么会这样显示? collectionView 单元格高度不高于实际 collectionView 本身。

假设您的单元格高 1000 磅,您将集合视图单元格高度设置为 995,并将集合视图顶部插图设置为 100,其中 returns 900。 所以问题是您的 collectionView 单元格比 collectionView 高 95pts。

您需要缩短 collectionView 单元格或降低 collectionView 顶部插图。