今天弄個 最新直播界面,想加個 header并级,記錄下拂檩。
創(chuàng)建一個UICollectionHeader 繼承自 UICollectionReusableView,并且同時創(chuàng)建他的xib文件嘲碧,記得xib要和 .swift 關聯(lián)稻励,接著 給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 比較來區(qū)分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 **最新的直播 ** 了。
截圖