我有一个UICollectionViewController
和要添加自定义标题。斯威夫特: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中的常规单元格重叠。
我在做什么错? 感谢任何帮助!
谢谢你,Magoo!使用FlowLayout让我走向正确的方向,'headerReferenceSize'完全符合我想要达到的目标。 '让flowLayout = self.collectionView?.collectionViewLayout as? UICollectionViewFlowLayout' – ixany
完美,很高兴你有它排序 – Magoo