ios - Swift-Uicollectionview- Collection View 中间的中心单元格

标签 ios swift2 uicollectionview uicollectionviewlayout

我试图将UICollectionView内部的所有单元格居中。单元格划分为11x4。

我尝试使用下面的代码,但是,单元格分组在一行中。

我试图实现的目标显示在第二张图片中:

错误
enter image description here

应该
enter image description here

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)
    }

最佳答案

如果您希望使用更直观的方法,请尝试更改插图
if you prefer a more visual approach try changing the insets

关于ios - Swift-Uicollectionview- Collection View 中间的中心单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38793916/

相关文章:

ios - 如何使用 NSSet 填充 tableView? ( swift )

arrays - 如何使用 swift 将自定义结构数组保存到 NSUserDefault?

ios - 将 UICollectionViewCell 放入 UICollectionView

ios - 在 UITableviewcell 中使用 Collectionview

ios - iPad屏幕右半部分的模态视图?

ios - 有没有办法阻止 UICollectionView 重复使用单元格?

ios - Swift2运行时实例化UIStoryboard,失败如何处理?

ios - 使用滚动(手势)重新加载 CollectionView

ios - 多少个 View Controller 是太多的 View Controller ?

ios - 当我在 Parse Swift 2.2 中遇到未经授权的错误时该怎么办