我试图将UICollectionView
内部的所有单元格居中。单元格划分为11x4。
我尝试使用下面的代码,但是,单元格分组在一行中。
我试图实现的目标显示在第二张图片中:
错误
应该
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let letterCell = collectionView.dequeueReusableCellWithReuseIdentifier("letterCell", forIndexPath: indexPath) as! LetterCellView
letterCell.center.y = self.view.center.y
return letterCell
}
同样在我的代码中,我正在使用
collectionViewLayout
将单元格划分为4行,也许我应该在这里实现居中系统,但是我无法获得实现该目标的方法。任何的想法?:func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
let totalCellWidth = 30 * 11
let totalSpacingWidth = 2 * (11 - 1)
let leftInset = (self.view.frame.size.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2;
let rightInset = leftInset
return UIEdgeInsetsMake(0, leftInset, 0, rightInset)
}
最佳答案
关于ios - Swift-Uicollectionview- Collection View 中间的中心单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38793916/