首页 > 解决方案 > UICollectionView 分页视图被切断 - 分页不起作用

问题描述

我正在实现一个需要水平分页的 UICollectionView。UICollectionViewCell 需要与 UICollectionView 的大小相同。该单元格有 2 个嵌入的行。第一个有一个 UITextView 和一个 UIButton,第二个有一个 UITextView。

UICollectionviewCell 和约束在预览中适用于不同尺寸的 iPhone(例如 4s、8、8plus),但是在模拟器中运行时分页无法正常工作,并且我猜单元格的宽度没有居中并且单元格没有移动得不够远,无法到达下一个单元格,从而使单元格看起来被截断。

我已经在这个 url 尝试了解决方案:UICollectionView Horizo​​ntal Paging not centered

但我似乎无法获得正确的宽度和居中。我正在使用的代码如下。

internal class AccessViewController : UIViewController,    UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

//...

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PagerContentCollectionViewCell

    //Set the cell title
    cell.txtTitle.text = "title"

    //add a target for the call capability
    cell.btnCall.addTarget(self, action: #selector(didButtonClick), for: .touchUpInside)

    //description
    cell.txtDescription.text =  "description"

    return cell
} 


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    //let i = IndexPath(item: titles.count, section: 0)
    collectionView.scrollToItem(at: indexPath, at: .right, animated: true)
    print("Selected")
    collectionView.reloadData()
}


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

    return 0
}

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


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let frameSize = collectionView.frame.size
    return CGSize(width: frameSize.width, height: frameSize.height)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

     return UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
}

UICollectionView 的宽度和高度分别为 267、153。UICollectioViewCell 的宽度和高度为 262、150,Section Insets 为 0,0,0,0。

任何帮助深表感谢。

谢谢

编辑:我添加了我试图用于布局的代码。我可能还缺少其他东西。

标签: iospaginationuicollectionviewuicollectionviewcellhorizontal-scrolling

解决方案


我能够使用这两个功能进行修复:

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

    return 0
}

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

推荐阅读