正如您正确指出的那样,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 并将其作为您的标头使用。