swift中UICollectionview的自定义页脚视图 [英] Custom Footer view for UICollectionview in swift
问题描述
我在swift中重写了一个min的Objective-C应用程序,并决定在我的情况下,集合视图比tableview更好。所以我的集合视图都设置了我想要的方式,现在我需要在我的集合视图中添加一个页脚。
I am re-writting an objective-C app of min in swift and decided that a collection view works better then a tableview in my case. So i have the collectionview all set up exactly how i want it and now i need to add a footer to my collection view.
在objective-C中它非常容易我,我所做的只是创建一个UIView,添加对象,然后将它添加到我的tableview中。
In objective-C it was very easy for me, all i did was create a UIView, add objects to it, and add it to my tableview like this.
self.videoTable.tableFooterView = footerView;
现在我一直在我的集合视图中尝试获得类似的解决方案,但是我已经有了到目前为止没有运气。
Now i've been trying in my collection view to get a similar solution, however i've had no luck so far.
是否有一个简单的.collectionviewFooterView或者我可以添加我的UIView的东西?
Is there a simple .collectionviewFooterView or something that i can add my UIView to?
编辑
i发现类似的想法这里如果有助于创建答案
EDIT i found similar ideas HERE if that helps create an answer
编辑
EDIT
我一直在考虑如何实现这一点,所以现在我的想法是使用以下代码将页脚视图添加到集合视图的末尾:
I've been thinking on ways to achieve this so right now my idea is to add the footer view to the end of the collection view using the following code:
var collectionHeight = self.collectionView.collectionViewLayout.collectionViewContentSize().height
footerView.frame = CGRectMake(0, collectionHeight, screenSize.width, 76)
我对此解决方法唯一的问题是我需要添加m将空间扩展到colletionView的contentView,我没有运气
The only issue i'm having with this workaround is i need to add more space to the contentView of the colletionView and i am having no luck with that
我的解决方案
My Solution
我使用解决方法解决了它(不是最漂亮的但它有效),我使用简单的
I solved it using a work-around (not the prettiest but it works), i add the UIView to the uicollectionview using a simple
footerView.frame = CGRectMake(0, collectionHeight - 50, screenSize.width, 50)
self.collectionView.addSubview(footerView)
并使用以下方法设置布局插图:
and set the layout insets using this:
alLayout.contentInsets = UIEdgeInsetsMake(2, 2, 52, 2)
推荐答案
集合视图处理页眉和页脚的方式与表视图不同。您需要:
Collection views handle headers and footers differently than table views. You'll need to:
-
使用以下方式注册页脚视图类:
Register your footer view class using:
registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView")
在集合视图布局上设置 headerReferenceSize
或实现 collectionView:layout:referenceSizeForHeaderInSection:
在您的委托
。
从<$ c $中的以下方法返回页脚视图c> 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
}
我认为这就是一切!
这篇关于swift中UICollectionview的自定义页脚视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!