如何在同一CollectionView中使用strechyHeader和stickyHeader?

时间:2019-04-18 12:52:39

标签: ios swift uicollectionview uicollectionviewlayout

在我的collectionView中,我希望第一个Header保持顺畅,而其他所有Header都应保持粘性。有没有办法做到这一点?这是strechy标头的代码

HeaderLayout类:UICollectionViewFlowLayout {

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    let layoutAttributes = super.layoutAttributesForElements(in: rect)


    let offset = collectionView!.contentOffset

    if offset.y < 0 {
        let deltaY = abs(offset.y)
        for attributes in layoutAttributes!{
            if let elementKind = attributes.representedElementKind {
                if elementKind == UICollectionView.elementKindSectionHeader && attributes.indexPath.section == 0 {
                    var frame = attributes.frame
                    frame.size.height = max(0, headerReferenceSize.height + deltaY)
                    frame.origin.y = frame.minY - deltaY
                    attributes.frame = frame
                }

            }
        }
    }

    return layoutAttributes
}

我尝试使用以下方法管理其他粘性标头:

       if elementKind == UICollectionView.elementKindSectionHeader && attributes.indexPath.section == 1 {
        sectionHeaderPinToVisibleBounds = true
    }

但是,一旦我设置了sectionHeaderPinToVisibleBounds属性,所有标题就很引人注目...

0 个答案:

没有答案