ios - UICollectionView 自定义 header 不显示 Swift 4

标签 ios swift uicollectionview

请帮忙,我一直在试图找出为什么我的代码不起作用。我想做的是为我的 Collection View 提供一个自定义 header ,但似乎我注册的类似乎没有触发。 它甚至没有显示红色框。它只显示青色空白背景

我做错了什么?

snapshot

下面是我的代码:

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/

相关文章:

iphone - NSUserDefaults 返回的共享默认对象的数据类型

ios - 使用 NavigationView 进行模态演示

iphone - UIDatePicker 获取 GTM 值

ios - UICollectionView 作为 iOS 中应用程序的 HEADER

ios - 将 NSTimer 与 UIProgressView 关联起来

ios - 恢复过期的自动续订订阅

iphone - 如何在 iOS 上每次打开唯一文件夹中的解压缩文件

ios - 函数无法正确执行(Swift 3)

ios - 在 performBatchUpdates 中重新加载 collectionview 时未调用 UICollectionview cellForItemAtIndexPath

ios - View Controller 中的嵌入式 UICollectionView 未被调用 Swift