我试图将所有细胞集中在UICollectionView
的中间。细胞分为11×4。
我尝试过使用下面的代码,然后将单元格分成一行。
我想要实现的目标显示在第二张图片中:
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)
}
答案 0 :(得分:2)
答案 1 :(得分:0)
找到解决方案:
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
let totalCellHeight = 30 * 4
let totalSpacingHeight = 5 * (4 - 1)
let topInset = (self.view.frame.size.height - CGFloat(totalCellHeight + totalSpacingHeight)) / 2;
let bottInset = topInset
return UIEdgeInsetsMake(topInset, leftInset, bottInset, rightInset)
}