请帮忙,我一直在试图找出为什么我的代码不起作用。我想做的是为我的 Collection View 提供一个自定义 header ,但似乎我注册的类似乎没有触发。 它甚至没有显示红色框。它只显示青色空白背景
我做错了什么?
下面是我的代码:
class VideoDetailController : UICollectionViewController, UICollectionViewDelegateFlowLayout {
private let headerId = "headerId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.cyan
collectionView?.register(VideoDetailHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerId)
navigationItem.title = "Hello!"
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionViewCell {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerId, for: indexPath) as! VideoDetailHeader
return header
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width,height:170)
}
}
class VideoDetailHeader: BaseCell {
override func setupViews() {
print("Setting up...")
super.setupViews()
backgroundColor = UIColor.red
}
}
BaseCell 是 UICollectionViewCell
最佳答案
上面的代码似乎可以工作: 唯一的问题是,当我调用这个 View Controller 时,我使用了: 让布局= UICollectionViewLayout() 代替 : 让布局 = UICollectionViewFlowLayout()
关于ios - UICollectionView 自定义 header 不显示 Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50805176/