今天弄个 最新直播界面,想加个 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 **最新的直播 ** 了。

网友评论