UICollectionView间距不相等

时间:2017-07-07 20:01:42

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

我有一个标准宽度和可变高度单元格的垂直display: inline-block。我的间距有问题。对于单元格的每个高度,单元格之后的间距是不同的:

UICollectionView的截图

1 个答案:

答案 0 :(得分:0)

UICollectionView具有delegate属性。您应该在某些类中实现UICollectionViewDelegateFlowLayout,例如View Controller。

这种方法负责间距:

// Space around the section.
func collectionView(_ collectionView: UICollectionView, 
                   layout collectionViewLayout: UICollectionViewLayout, 
                   insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
}

// In case of vertical layout this is a vertical space between horizontal rows inside of each section
func collectionView(_ collectionView: UICollectionView, 
                    layout collectionViewLayout: UICollectionViewLayout, 
                    minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 20
}

// Horizontal space between items in row
func collectionView(_ collectionView: UICollectionView, 
                    layout collectionViewLayout: UICollectionViewLayout,  
                    minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 20
}