Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CustomFlowLayout: UICollectionViewFlowLayout {
- override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
- guard let collectionView = collectionView else {
- return nil
- }
- var layoutAttributes = super.layoutAttributesForElements(in: rect)
- // Adjust header position
- layoutAttributes?.forEach { attributes in
- if attributes.representedElementKind == UICollectionView.elementKindSectionHeader {
- let contentOffsetY = collectionView.contentOffset.y
- let headerHeight = attributes.frame.size.height
- var origin = attributes.frame.origin
- // Pin header to top
- origin.y = min(max(contentOffsetY, 0), headerHeight)
- attributes.frame = CGRect(origin: origin, size: attributes.frame.size)
- }
- }
- return layoutAttributes
- }
- override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
- return true // Invalidate layout when bounds change
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement