如何在组合布局中创建弹性标题?

sky*_*lin 5 uicollectionview uicollectionviewlayout swift uicollectionviewflowlayout uicollectionviewcompositionallayout

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
    }

}
Run Code Online (Sandbox Code Playgroud)

尝试将其实现为组合布局,但到目前为止效果不佳。我怎样才能在构图布局中实现这一目标?