美文网首页
Swift - 给你的Collectionview加个头尾

Swift - 给你的Collectionview加个头尾

作者: BeijingIamback | 来源:发表于2016-08-06 14:16 被阅读2171次

好文👌
Collection View Basics
纯代码构建Collectionview的header

今天弄个 最新直播界面,想加个 header,记录下。

创建一个UICollectionHeader 继承自 UICollectionReusableView,并且同时创建他的xib文件,记得xib要和 .swift 关联,接着 给header.xib 定义一个 identifier为 "header"。

截图

想要干的事儿都在xib上干了。

回到vc 界面,初始化你的layout

// 设定header的大小
layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 33)

// 在注册cell 的同时,别忘了注册header
let nib = UINib.init(nibName: "UICollectionHeader", bundle: nil)
self.collectionView.registerNib(nib, forSupplementaryViewOfKind: 
UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
// 头视图,这里其实应该使用这个
//UICollectionElementKindSectionHeader和 kind 比较来区分header 和footer
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
            let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath)
            return headerView
    }

接下来就显示出header **最新的直播 ** 了。

截图

相关文章

网友评论

      本文标题:Swift - 给你的Collectionview加个头尾

      本文链接:https://www.haomeiwen.com/subject/ylmksttx.html