如何设置UICollectionView的CellSize,MinSpacing和SectionInset

时间:2018-07-02 11:12:01

标签: ios swift uicollectionview

如何在“代码”上设置UICollectionView的CellSize,MinSpacing和SectionInset?

enter image description here

它在图像的红框内。

2 个答案:

答案 0 :(得分:0)

所有这些都是流程布局的属性,我们必须在集合视图开始布局单元格之前对其进行设置,这也可以通过编程方式设置

let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout

现在您可以使用上方的layout变量来设置通过属性检查器设置的所有参数

答案 1 :(得分:0)

您应该使用UICollectionViewFlowLayout 使用以下代码作为参考。

let cellSide: CGFloat = yourCustomCellSide
let leftOrRightMargin: CGFloat = yourCustomSideMargin
let sizeRect = UIScreen.main.bounds
let deviceWidth = sizeRect.size.width
let availableWidth = deviceWidth - (cellSide * 4)

let customFlowLayout = UICollectionViewFlowLayout()
customFlowLayout.scrollDirection = .horizontal
customFlowLayout.sectionInset = UIEdgeInsets(top: 10, left: leftOrRightMargin, bottom: 10, right: leftOrRightMargin)
customFlowLayout.minimumInteritemSpacing = 0
customFlowLayout.minimumLineSpacing = availableWidth/4
customFlowLayout.itemSize = CGSize(width: cellSide, height: cellSide)
yourCollectionView.collectionViewLayout = customFlowLayout