【问题标题】:Center a collection view when scrolling with partially visible previous and next item滚动部分可见的上一个和下一个项目时将集合视图居中
【发布时间】:2020-11-24 10:09:09
【问题描述】:

我想达到这个图像中的结果,这就是我在启动集合视图(第一项)时得到的结果:

但是当我左右滚动插入时,似乎不再尊重并且项目不再像这样居中:

我的部分代码:

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

return  CGSize(width: view.bounds.size.width - 60 , height: 450)

}

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

}

【问题讨论】:

    标签: ios swift collectionview insets


    【解决方案1】:

    您好,我确实了解您要求,只要将集合视图滚动一点,您就必须完全显示下一个单元格。

    为了实现,collectionView 中会显示单元格

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    if !(indexPath.section == 0), !(indexPath.row == 0) {
        let indexPath = IndexPath(item: indexPath.row + 1, section: indexPath.section)
            self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: true)
     }
    }
    

    滚动到下一个项目。

    希望这对你有用:)

    【讨论】:

    • 感谢您的回复,但我上传的第二张图片显示了第二项的收藏视图。我没有像图像中那样滚动一点。那么现在发生了什么,当我滚动到另一个项目时,我没有完全看到该项目。我想完整地显示当前项目以及一些上一个项目和一些下一个项目,就像我上传的第一张图片一样
    猜你喜欢
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多