我希望 UICollectionView 的项目位于中心,所以我编写了以下代码:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
let collectionViewWidth = collectionView.bounds.width
let collectionViewHeight = collectionView.bounds.height
let numberOfRows = CGFloat(collectionView.numberOfItemsInSection(0))
let leftValue = (collectionViewWidth / 2.0) - (numberOfRows * 5) - (numberOfRows * (collectionViewHeight / 2.0))
print(leftValue)
return UIEdgeInsets(top: 0, left: leftValue, bottom: 0, right: 0)
}
但是现在的问题是我无法将其滚动到左侧,因为我的左侧插图发生了变化,请建议我任何解决方案。