在单元之间的UICollectionView中设置/覆盖填充

我有一个UICollectionView,我遇到了单元格之间的填充问题。 从理论上讲,我应该可以将屏幕分成4份,我可以得到一个有4张图像的细胞大小,它们完全占据屏幕宽度。 但是,它select不这样做。 相反,它会创build3个带有巨大填充的图像,如下所示:

_cellSize = CGSizeMake(UIScreen.mainScreen().bounds.width/4,UIScreen.mainScreen().bounds.width/4) 

在这里输入图像说明

所以我减less了单元格大小来创build一些填充,这是更接近,但我需要大约一半的填充:

 _cellSize = CGSizeMake(UIScreen.mainScreen().bounds.width/4.4,UIScreen.mainScreen().bounds.width/4.4) 

在这里输入图像说明

所以我试着让单元格更大一些,然后翻到下一行,并把3个项目再次填充为HUGE。

 _cellSize = CGSizeMake(UIScreen.mainScreen().bounds.width/4.3,UIScreen.mainScreen().bounds.width/4.3) 

在这里输入图像说明

我可以指定单元格之间的填充没有它决定,如果填充低于“X”数量,它创build一个新的行,而不是? 就像我可以设置一个新的行之前的阈值填充说,0?

我想让它变得如此薄弱: 在这里输入图像说明

你只需要做正确的计算,包括你想要的边缘空间以及单元格之间的空间。 例如,如果你想要3个单元格的5个点到边缘和单元格之间,你应该有这样的东西,

 override func viewDidLoad() { super.viewDidLoad() var layout = self.collectionView.collectionViewLayout as UICollectionViewFlowLayout layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5); layout.minimumInteritemSpacing = 5; // this number could be anything <=5. Need it here because the default is 10. layout.itemSize = CGSizeMake((self.collectionView.frame.size.width - 20)/3, 100) // 20 is 2*5 for the 2 edges plus 2*5 for the spaces between the cells }