swift中UICollectionview的自定义页脚视图

inV*_*ble 8 iphone ios uicollectionview swift

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

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

self.videoTable.tableFooterView = footerView;
Run Code Online (Sandbox Code Playgroud)

现在我一直在我的集合视图中尝试获得类似的解决方案,但到目前为止我没有运气.

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

编辑 我发现类似的想法这里有没有什么帮助创建一个答案

编辑

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

var collectionHeight = self.collectionView.collectionViewLayout.collectionViewContentSize().height

footerView.frame = CGRectMake(0, collectionHeight, screenSize.width, 76)
Run Code Online (Sandbox Code Playgroud)

我有这个解决方法的唯一问题是我需要为colletionView的contentView添加更多空间,我没有运气

我的解决方案

我使用解决方法解决了它(不是最漂亮的但它有效),我使用简单的方法将UIView添加到uicollectionview

footerView.frame = CGRectMake(0, collectionHeight - 50, screenSize.width, 50)
self.collectionView.addSubview(footerView)
Run Code Online (Sandbox Code Playgroud)

并使用以下设置布局插图:

alLayout.contentInsets = UIEdgeInsetsMake(2, 2, 52, 2)
Run Code Online (Sandbox Code Playgroud)

Nat*_*ook 11

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

  1. 使用以下命令注册页脚视图类:

    registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView")
    
    Run Code Online (Sandbox Code Playgroud)
  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
    }
    
    Run Code Online (Sandbox Code Playgroud)

我认为这就是一切!