swift中UICollectionview的自定义页脚视图

时间:2014-11-12 16:52:54

标签: ios iphone swift uicollectionview

我在swift中重写了一个min的Objective-C应用程序,并决定在我的情况下,集合视图比tableview更好。所以我的集合视图都设置了我想要的方式,现在我需要在我的集合视图中添加一个页脚。

在Objective-C中,对我来说非常容易,我所做的只是创建一个UIView,向它添加对象,然后将它添加到我的tableview中。

self.videoTable.tableFooterView = footerView;

现在我一直在尝试使用我的收藏视图来获得类似的解决方案,但到目前为止我还没有运气。

是否有一个简单的.collectionviewFooterView或者我可以添加我的UIView的东西?

修改 我找到了类似的想法HERE,如果这有助于创建答案

修改

我一直在考虑如何实现这一点,所以现在我的想法是使用以下代码将页脚视图添加到集合视图的末尾:

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)

1 个答案:

答案 0 :(得分:11)

集合视图处理页眉和页脚的方式与表格视图不同。您需要:

  1. 使用以下方式注册页脚视图类:

    registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView")
    
  2. 在您的收藏视图布局上设置headerReferenceSize或在collectionView:layout:referenceSizeForHeaderInSection:中实施delegate

  3. 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
    }
    
  4. 我认为这就是一切!