我有一个带有页眉和页脚的collectionView。我想在本节的末尾添加空格。
我正在使用:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 16, right: 0)
}
但是,这不考虑页脚视图。因此,插图将添加到该部分的最后一个索引中。在应用插图之前,是否有任何方法或方法可以包括节脚注?
我需要在页脚视图下插入图片。
现有功能的说明。 (黄色是indexPath.item)。预期的功能是将页脚下的插图(红色)放置。
答案 0 :(得分:0)
正如您正确指出的那样,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并将其出队作为标题。