Advertisement
astrou123

Untitled

Apr 11th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. class CustomFlowLayout: UICollectionViewFlowLayout {
  2.  
  3. override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  4. guard let collectionView = collectionView else {
  5. return nil
  6. }
  7.  
  8. var layoutAttributes = super.layoutAttributesForElements(in: rect)
  9.  
  10. // Adjust header position
  11. layoutAttributes?.forEach { attributes in
  12. if attributes.representedElementKind == UICollectionView.elementKindSectionHeader {
  13. let contentOffsetY = collectionView.contentOffset.y
  14. let headerHeight = attributes.frame.size.height
  15. var origin = attributes.frame.origin
  16.  
  17. // Pin header to top
  18. origin.y = min(max(contentOffsetY, 0), headerHeight)
  19.  
  20. attributes.frame = CGRect(origin: origin, size: attributes.frame.size)
  21. }
  22. }
  23.  
  24. return layoutAttributes
  25. }
  26.  
  27. override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
  28. return true // Invalidate layout when bounds change
  29. }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement