![](/img/trans.png)
[英]How to add header and footer view of UICollectionView in Swift
[英]Custom Footer view for UICollectionview in swift
我正在 swift 重写一个 min 的objective-C 应用程序,并决定集合视图在我的情况下比 tableview 效果更好。 所以我已经完全按照我想要的方式设置了集合视图,现在我需要在我的集合视图中添加一个页脚。
在 Objective-C 中,这对我来说很容易,我所做的就是创建一个 UIView,向其中添加对象,然后像这样将其添加到我的 tableview 中。
self.videoTable.tableFooterView = footerView;
现在我一直在我的收藏视图中尝试获得类似的解决方案,但是到目前为止我没有运气。
是否有一个简单的.collectionviewFooterView
或我可以添加我的 UIView 的东西?
编辑我在这里找到了类似的想法,如果这有助于创建答案
编辑
我一直在思考如何实现这一点,所以现在我的想法是使用以下代码将页脚视图添加到集合视图的末尾:
var collectionHeight = self.collectionView.collectionViewLayout.collectionViewContentSize().height
footerView.frame = CGRectMake(0, collectionHeight, screenSize.width, 76)
我在这个解决方法中遇到的唯一问题是我需要为 colletionView 的 contentView 添加更多空间,但我对此并不走运
我的解决方案
我使用变通方法解决了它(不是最漂亮但它有效),我使用一个简单的方法将 UIView 添加到 uicollectionview
footerView.frame = CGRectMake(0, collectionHeight - 50, screenSize.width, 50)
self.collectionView.addSubview(footerView)
并使用此设置布局插图:
alLayout.contentInsets = UIEdgeInsetsMake(2, 2, 52, 2)
集合视图处理页眉和页脚的方式与表格视图不同。 你需要:
使用以下方法注册页脚视图类:
registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView")
在您的集合视图布局上设置headerReferenceSize
或在您的delegate
实现collectionView:layout:referenceSizeForHeaderInSection:
。
从dataSource
的以下方法返回页脚视图:
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { let view = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView", forIndexPath: indexPath) // configure footer view return view }
我认为这就是一切!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.