我正在尝试为自定义流布局(PinterestLayout)实现标头视图,但标头显示在其他单元格的顶部。我在做什么错了?
这是我的实现方式
在override func prepare()
上,我将elementKindSectionHeader
附加到缓存
let headerAtrributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, with: IndexPath(item: 0, section: 0))
cache.append(headerAtrributes)
然后设置框架
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes = [UICollectionViewLayoutAttributes]()
for attribute in cache {
if attribute.frame.intersects(rect) {
if attribute.representedElementKind == UICollectionView.elementKindSectionHeader {
attribute.frame = CGRect(x: 0, y: 0, width: collectionView!.frame.width, height: 400)
}
layoutAttributes.append(attribute)
}
}
return layoutAttributes
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return cache[indexPath.item]
}
结果 Check image