在UICollectionView中调整特定部分的insets

时间:2016-08-09 18:38:58

标签: ios swift uicollectionview uicollectionviewlayout uicollectionviewdelegate

  1. insetForSectionAtIndex(在DelegateFlowLayout上)可以为一个部分内的所有单元格设置插入内容

  2. sectionInset(在FlowLayout上)允许用户设置适用于所有部分的插图。

  3. 但是,我正在寻找一种方法,只将插件应用于一个特定的部分 - 这可能吗?

3 个答案:

答案 0 :(得分:4)

您必须使用此方法向您的班级实施UICollectionViewDelegateFlowLayout

适用于Swift 4

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

    var edgeInsets = UIEdgeInsets()
    if section == THE_SECTION_YOU_WANT {
        // edgeInsets configuration stuff
    }

    return edgeInsets;
}

适用于Swift 3

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {

    var edgeInsets = UIEdgeInsets()
    if section == THE_SECTION_YOU_WANT {
        // edgeInsets configuration stuff
    }

    return edgeInsets;

}

答案 1 :(得分:1)

swift 4的

命名为:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
...

答案 2 :(得分:1)

您可以通过UICollectionViewDelegateFlowLayout协议执行以下操作:

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt section: Int) -> UIEdgeInsets {

        switch section {
        case 0:
            return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        case 1:
            return UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
        }
    }