Я делаю пользовательский 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
}
}