【发布时间】:2017-02-21 10:19:58
【问题描述】:
我正在尝试将一个数字(页码)从视图控制器传递到另一个视图控制器,该控制器具有 UICollectionView 和 paging enabled 选项。我的代码确实滚动到一个页面,但内容没有居中,这是我的代码:
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)
}
结果:
我需要什么:
解决方案:
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)
}
【问题讨论】:
标签: ios iphone swift xcode uicollectionview