Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class CustomFlowLayout: UICollectionViewFlowLayout {
- override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
- guard let collectionView = collectionView else { return nil }
- // Retrieve the layout attributes from the super class
- var layoutAttributes = super.layoutAttributesForElements(in: rect)
- // Check if refresh control is refreshing and adjust header position if needed
- if let refreshControl = collectionView.refreshControl, refreshControl.isRefreshing {
- for attributes in layoutAttributes ?? [] {
- // Adjust header attributes if the element is a section header
- if attributes.representedElementKind == UICollectionView.elementKindSectionHeader {
- let section = attributes.indexPath.section
- let headerHeight = headerReferenceSize.height
- let contentOffset = collectionView.contentOffset.y + collectionView.contentInset.top
- let topInset = collectionView.adjustedContentInset.top
- // Calculate the y position of the header
- var headerY = max(contentOffset, topInset)
- if section > 0 {
- // Adjust header position based on previous sections' height
- for i in 0..<section {
- let previousHeaderHeight = headerReferenceSize.height
- let previousSectionItemCount = collectionView.numberOfItems(inSection: i)
- let previousSectionHeight = (CGFloat(previousSectionItemCount) * itemSize.height) + previousHeaderHeight + minimumLineSpacing
- headerY += previousSectionHeight
- }
- }
- // Update the position of the header attributes
- attributes.frame = CGRect(x: attributes.frame.origin.x, y: headerY, width: attributes.frame.width, height: headerHeight)
- }
- }
- }
- return layoutAttributes
- }
- override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
- return true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement