简体   繁体   English

UICollectionView 水平滚动无法正常工作

[英]UICollectionView horizontal scroll not working properly

I am trying to install a collectionView that has cells who scroll horizontally.我正在尝试安装一个具有水平滚动单元格的 collectionView。 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,我不知道为什么在我尝试与之交互时 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)
  }

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. UICollectionViewFlowLayout 的行为未定义,因为:项目高度必须小于 UICollectionView 的高度减去部分插入顶部和底部值,减去内容插入顶部和底部值。

Why does this show?为什么会显示这个? The collectionView cells height are not taller than the actual collectionView itself. collectionView 单元格的高度不高于实际的 collectionView 本身。

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.假设您的单元格高 1000pts,您将集合视图单元格高度设置为 995,并将集合视图顶部插入设置为 100,返回 900。所以问题是您的 collectionView 单元格比 collectionView 高 95pts。

You need to make your collectionView cell shorter or lower the collectionView top inset.您需要缩短 collectionView 单元格或降低 collectionView 顶部插图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM