【问题标题】:CollectionViewSection Inset Does Not Consider Header/Footer ViewsCollectionViewSection Inset 不考虑页眉/页脚视图
【发布时间】:2020-09-02 23:10:34
【问题描述】:

我有一个带有页眉和页脚的 collectionView。我想在部分末尾添加间距。

我正在使用:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 16, right: 0)
}

但是,这不考虑页脚视图。因此,插入被添加到该部分的最后一个索引。是否有任何方法或方式在应用插图之前包含节页脚?

我需要页脚视图下的插图。

现有功能的说明。 (黄色是 indexPath.item)预期的功能是在页脚下插入(红色)。

【问题讨论】:

    标签: ios swift uicollectionview uicollectionreusableview uicollectionviewflowlayout


    【解决方案1】:

    正如您正确指出的那样,UICollectionView 的部分位于页眉和页脚之间。

    如果您想在页脚下方添加间距,最好的方法是创建自定义页脚视图。

    optional func collectionView(_ collectionView: UICollectionView, 
    viewForSupplementaryElementOfKind kind: String, 
                              at indexPath: IndexPath) -> UICollectionReusableView
    

    Here 是此方法的文档。

    理想情况下,您会希望为您的自定义页脚创建一个子类:]

    class CustomFooterView: UICollectionReusableView {
    
    }
    

    然后,在viewDidLoad注册:

    collectionView.register(CustomFooterView.self, forSupplementaryViewOfKind: .elementKindSectionFooter, withReuseIdentifier: "Your footer reuse ID")
    

    然后,你可以这样返回它:

    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionFooter {
            return collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Your footer reuse ID", for: indexPath)
        }
        else {
        }
    }
    

    现在您只需要确保自动布局能够确定页脚的大小。因此,为您的自定义页脚添加标签并确保其底部约束等于您所需的填充。

    注意:有一个警告。现在您实现了返回自定义页脚的方法,并且为页眉调用了相同的方法,您可能还需要使用自定义页眉。或者,您需要注册标准 UICollectionReusableView 并将其作为您的标头使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-13
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 2013-05-07
      相关资源
      最近更新 更多