ios - 使用部分可见的上一个和下一个项目滚动时将集合视图居中
问题描述
我想达到这个图像中的结果,这就是我启动集合视图(第一项)时得到的结果:
但是,当我左右滚动插图时,似乎不再尊重并且该项目不再像这样居中:
我的部分代码:
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)
}
解决方案
嗨,我确实了解您要求只要将集合视图滚动一点,您就必须完全显示下一个单元格。
为了在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)
}
}
滚动到下一个项目。
希望这对你有用:)
推荐阅读
- c# - 全屏时如何使项目在Windows窗体中自动调整大小
- c# - Try and catch 块未捕获格式异常
- angular - 如何隐藏默认的 HTML 附加文件夹按钮;但是在我们的自定义按钮中使用该功能
- html - 溢出:滚动容器缩小孩子而不是让他们根据需要扩大
- arcore - 不支持 CPU 镜像访问
- c# - 使用 Aspose PDF .Net 时添加文本框字段会引发“IndexOutBoundsException”
- aws-cdk - 如何在 AWS CodeBuild 中设置 AWS CDK 应用程序执行?
- ios - CollectionView 在我的聊天视图中按顺序更新和复制。(Swift 4.2,Xcode)(MessengerKit)
- docker - 拉取图像和从 tar 文件加载图像的区别
- php - 当我使用 Linux task-crontab 时如何同时解决多个问题