gpt4 book ai didi

ios - 滚动到启用分页的 UICollectionView 中的页面

转载 作者:搜寻专家 更新时间:2023-10-31 19:33:00 25 4
gpt4 key购买 nike

我正在尝试将一个数字(页码)从一个 View Controller 传递到另一个具有 UICollectionViewpaging enabled 选项的 View Controller 。我的代码确实滚动到一个页面,但内容没有居中,这是我的代码:

override func viewWillAppear(_ animated: Bool) {
let currentPage = 4
let indexPath = NSIndexPath(row: currentPage , section: 0) as IndexPath
self.collectionView.scrollToItem(at: indexPath, at: .right, animated: true)
}

结果:

enter image description here

我需要什么:

enter image description here

解决方案:

collectionView.setContentOffset(CGPoint(x:  CGFloat(currentPage) * collectionView.bounds.size.width , y: 0), animated: false)

已编辑

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

return CGSize(width: 321 , height: 321)

}

最佳答案

你可以尝试像这样滚动:

collectionView.setContentOffset(CGPoint(x:  currentPage * itemSize, y: 0), animated: false)

其中 itemSize 是 Collection View 中项目的大小。此外,如果每个项目之间有间距,则在计算 x

时也应添加该间距

编辑:您需要使用 itemSize.width,以防您需要像这样将 currentPage 转换为 CGFloat CGFloat(currentPage)

关于ios - 滚动到启用分页的 UICollectionView 中的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42364591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com