【发布时间】:2020-04-01 16:06:40
【问题描述】:
我正在制作自定义 UICollectionViewFlowLayout,并希望我的第二个也是唯一的第二个标题在滚动期间“固定”到顶部,从而创建浮动标题效果。但是,在我当前的代码中,标题是固定的,但在一定的内容偏移量后会消失,但如果您向后滚动,则会重新出现。
class StickyLayout: UICollectionViewFlowLayout {
let kind = UICollectionView.elementKindSectionHeader
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let layoutAttributes = super.layoutAttributesForElements(in: rect)
layoutAttributes?.forEach({ (attributes) in
if(attributes.representedElementKind == kind && attributes.indexPath.section == 1) {
guard let collectionView = collectionView else { return }
let contentOffset = collectionView.contentOffset.y
var headerFrame = attributes.frame
headerFrame.size.height = 90
if(contentOffset > 300) {
headerFrame.origin.y = contentOffset
attributes.frame = headerFrame
}
}
})
return layoutAttributes
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
}
【问题讨论】:
-
嗨,尼克,请阅读并考虑这个stackoverflow.com/help/how-to-ask。请给我们一个可复制的可复制示例,以便我们检查您在做什么。