ios - swift 中 UICollectionview 的自定义页脚 View

标签 ios iphone swift uicollectionview

我正在快速重写 min 的 objective-C 应用程序,并决定在我的案例中 Collection View 比 TableView 更好。所以我已经完全按照我想要的方式设置了 Collection View ,现在我需要在我的 Collection View 中添加一个页脚。

在 objective-C 中,这对我来说非常容易,我所做的就是创建一个 UIView,向其中添加对象,然后像这样将其添加到我的 tableview。

self.videoTable.tableFooterView = footerView;

现在我一直在我的 Collection View 中尝试获得类似的解决方案,但是到目前为止我没有运气。

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

编辑 我发现了类似的想法 HERE如果这有助于创建答案

编辑

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

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)

最佳答案

Collection View 处理页眉和页脚的方式与 TableView 不同。你需要:

  1. 使用以下方式注册您的页脚 View 类:

    registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView")
    
  2. 要么设置headerReferenceSize在您的收藏 View 布局或实现 collectionView:layout:referenceSizeForHeaderInSection:在你的delegate .

  3. 通过您的 dataSource 中的以下方法返回页脚 View :

    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
    }
    

我想这就是一切!

关于ios - swift 中 UICollectionview 的自定义页脚 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26892247/

相关文章:

iphone - 什么是 ASIHTTPRequest?

iphone - iAd "Too many active banners, Creation of new banners will be throttled."警告

iphone - 从 alertview 委托(delegate)方法 : - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 返回 BOOL 值

ios - 如何根据 ios 中的设备宽度换行到下一行

iphone - 如何旋转imageview而不卡顿?

ios - 带有泛型的Swift Codable,可保存响应中的通用数据

swift - 如何在 swift 中克隆像 Java 这样的对象

ios - 如何在漫长的过程中使用事件指示器?

ios - 使用按钮执行 SCNAction (SwiftUI)

php - iOS - 使用相同的消息和有效负载同时从同一服务器向多个应用程序发送推送通知的正确方法