我有一个UICollectionViewController,想添加一个自定义标头。

我在CollectionViewController的Interface Builder中检查了Section Header,并在其子类中实现了viewForSupplementaryElementOfKind函数。

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    switch kind {

    case UICollectionElementKindSectionHeader:

        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "myHeaderIdentifier", for: indexPath)

        // !! NOT WORKING PROPERLY
        // sectionHeader overlaps other cells

        headerView.backgroundColor = UIColor(hue: 0.9, saturation: 1.0, brightness: 0.9, alpha: 1.0)
        headerView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 250.0)

        return headerView

    default:
        assert(false, "Unexpected element kind")
    }

}

更改生效,但是标题(下面的屏幕截图中的粉红色区域)现在与collectionView中的常规单元格重叠。

ios - Swift:UICollectionViewController的标题与单元格重叠-LMLPHP

我究竟做错了什么?
感谢任何帮助!

最佳答案

public func layoutAttributesForSupplementaryElementOfKind(kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes?

我相信在这里您应该能够设置框架/高度

如果headerReferenceSize是静态的,则可以在UICollectionViewFlowLayout中设置collectionView

关于ios - Swift:UICollectionViewController的标题与单元格重叠,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40678300/

10-09 12:54