1
class StretchyHeader: UICollectionViewFlowLayout {

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

        let layoutAttributes = super.layoutAttributesForElements(in: rect)! as [UICollectionViewLayoutAttributes]
        let contentOffset = collectionView!.contentOffset

        if (contentOffset.y < 0) {
            let deltaY = abs(contentOffset.y)
            for attributes in layoutAttributes where  attributes.representedElementKind == UICollectionView.elementKindSectionHeader {
                var frame = attributes.frame
                    frame.size.height =  headerReferenceSize.height + deltaY
                    frame.origin.y = frame.minY - deltaY
                    attributes.frame = frame
            }
        }

        return layoutAttributes
    }

    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }

}

试图将其实现为组合布局,但到目前为止还没有成功。如何在组合布局中实现这一点?

4

0 回答 0