How to make Supplementary View float in UICollectionView as Section Headers do in UITableView plain style

后端 未结 13 748
逝去的感伤
逝去的感伤 2020-11-27 09:35

I\'m struggling to achieve a \"floating section header\" effect with UICollectionView. Something that\'s been easy enough in UITableView (default b

13条回答
  •  孤街浪徒
    2020-11-27 10:11

    There is a bug in cocotouch's post. When there is no items in section and the section footer were set to be not displayed, the section header will go outside of the collection view and the user will be not able to see it.

    In fact change:

    if (numberOfItemsInSection > 0) {
        firstObjectAttrs = [self layoutAttributesForItemAtIndexPath:firstObjectIndexPath];
        lastObjectAttrs = [self layoutAttributesForItemAtIndexPath:lastObjectIndexPath];
    } else {
        firstObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                atIndexPath:firstObjectIndexPath];
        lastObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter
                                                               atIndexPath:lastObjectIndexPath];
    }
    

    into:

    if (numberOfItemsInSection > 0) {
        firstObjectAttrs = [self layoutAttributesForItemAtIndexPath:firstObjectIndexPath];
        lastObjectAttrs = [self layoutAttributesForItemAtIndexPath:lastObjectIndexPath];
    } else {
        firstObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                atIndexPath:firstObjectIndexPath];
        lastObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter
                                                               atIndexPath:lastObjectIndexPath];
        if (lastObjectAttrs == nil) {
            lastObjectAttrs = firstObjectAttrs;
        }
    }
    

    will solve this issue.

提交回复
热议问题