Advertisement
astrou123

Untitled

Mar 26th, 2024
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.31 KB | None | 0 0
  1. import UIKit
  2.  
  3. class CustomFlowLayout: UICollectionViewFlowLayout {
  4.    
  5.     override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  6.         guard let collectionView = collectionView else { return nil }
  7.        
  8.         // Retrieve the layout attributes from the super class
  9.         var layoutAttributes = super.layoutAttributesForElements(in: rect)
  10.        
  11.         // Check if refresh control is refreshing and adjust header position if needed
  12.         if let refreshControl = collectionView.refreshControl, refreshControl.isRefreshing {
  13.             for attributes in layoutAttributes ?? [] {
  14.                 // Adjust header attributes if the element is a section header
  15.                 if attributes.representedElementKind == UICollectionView.elementKindSectionHeader {
  16.                     let section = attributes.indexPath.section
  17.                     let headerHeight = headerReferenceSize.height
  18.                     let contentOffset = collectionView.contentOffset.y + collectionView.contentInset.top
  19.                     let topInset = collectionView.adjustedContentInset.top
  20.                    
  21.                     // Calculate the y position of the header
  22.                     var headerY = max(contentOffset, topInset)
  23.                     if section > 0 {
  24.                         // Adjust header position based on previous sections' height
  25.                         for i in 0..<section {
  26.                             let previousHeaderHeight = headerReferenceSize.height
  27.                             let previousSectionItemCount = collectionView.numberOfItems(inSection: i)
  28.                             let previousSectionHeight = (CGFloat(previousSectionItemCount) * itemSize.height) + previousHeaderHeight + minimumLineSpacing
  29.                             headerY += previousSectionHeight
  30.                         }
  31.                     }
  32.                    
  33.                     // Update the position of the header attributes
  34.                     attributes.frame = CGRect(x: attributes.frame.origin.x, y: headerY, width: attributes.frame.width, height: headerHeight)
  35.                 }
  36.             }
  37.         }
  38.        
  39.         return layoutAttributes
  40.     }
  41.    
  42.     override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
  43.         return true
  44.     }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement