Correct Collection View "stretchy header" implementation?

Hello, I have the following subclass of UICompositionalCollectionViewLayout to get the stretchy header effect as shown below.

It works quite well, but I don't really have an experience with creating custom layouts so I thought I'd ask if maybe my implementation doesn't have some important flaws.

I once ran into persistent layout loop crash with this and I am not sure what exactly I changed but it stopped happening. However since I am using this layout on important screen, I would like to make sure there isn't obvious potential for the layout loop crash happening in App Store version.

I am particularly unsure about the shouldInvalidateLayout implementation. Originally I was returning true all the time, but decided to change it and only force invalidation for negative content offset which is when my header is supposed to stretch.

Here is the full code:

final class StretchyCompositionalLayout: UICollectionViewCompositionalLayout {
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var attrs = super.layoutAttributesForElements(in: rect) ?? []
        
        guard let collectionView = collectionView else {
            return attrs
        }
        
        let contentOffset = collectionView.contentOffset.y
        
        guard contentOffset < 0 else {
            return attrs
        }
        
        var newAttributes: UICollectionViewLayoutAttributes?
        
        attrs.forEach({ attribute in
            if attribute.indexPath.section == 0 && attribute.indexPath.item == 0 {
                let startFrame = attribute.frame
                
                newAttributes = attribute.copy() as? UICollectionViewLayoutAttributes
                
                let newFrame: CGRect = .init(x: 0, y: contentOffset, width: startFrame.width, height: startFrame.height - contentOffset)
                newAttributes?.frame = newFrame
            }
        })
        
        if let new = newAttributes {
            attrs.removeAll { attr in
                return attr.indexPath.section == 0 && attr.indexPath.item == 0
            }
            
            attrs.insert(new, at: 0)
        }
        
        return attrs
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        guard let attributes = super.layoutAttributesForItem(at: indexPath) else {
            return nil
        }
        
        let contentOffset = collectionView?.contentOffset.y ?? 1
        
        guard contentOffset < 0 else {
            return attributes
        }
        
        if indexPath.section == 0 && indexPath.item == 0 {
            let attributes = attributes.copy() as? UICollectionViewLayoutAttributes ?? attributes
            
            let startFrame = attributes.frame
            
            let newFrame: CGRect = .init(x: 0, y: contentOffset, width: startFrame.width, height: startFrame.height - contentOffset)
            attributes.frame = newFrame
            
            return attributes
        } else {
            return super.layoutAttributesForItem(at: indexPath)
        }
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        let contentOffset = collectionView?.contentOffset.y ?? 1
        
        // There is visual glitch when 0 is used in this condition
        if contentOffset < 1 {
            return true
        } else {
            return super.shouldInvalidateLayout(forBoundsChange: newBounds)
        }
    }
}

Any feedback welcome!

Correct Collection View "stretchy header" implementation?
 
 
Q