UICollectionView Floating Headers on Top and Side

后端 未结 2 1502
别跟我提以往
别跟我提以往 2021-02-06 11:42

How do you implement headers in a UICollectionView? I know you can put in supplementary views, but I don\'t know how to make them \"float\" above a section like hea

相关标签:
2条回答
  • 2021-02-06 12:08

    Update for iOS9:

    let flow = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    flow.sectionHeadersPinToVisibleBounds = true
    

    Pass it along.

    0 讨论(0)
  • 2021-02-06 12:19

    EDIT: As mentioned in the comments, this answer is valid, but not for multiple sections. See this blog for a better solution: http://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-using#notes


    You need to specify that behaviour in your layout:

    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    {
      NSMutableArray* attributesArray = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    
      BOOL headerVisible = NO;
    
      for (UICollectionViewLayoutAttributes *attributes in attributesArray) {
        if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
          headerVisible = YES;
          attributes.frame = CGRectMake(self.collectionView.contentOffset.x, 0, self.headerReferenceSize.width, self.headerReferenceSize.height);
          attributes.alpha = HEADER_ALPHA;
          attributes.zIndex = 2;
        }
      }
    
      if (!headerVisible) {
        UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                                            atIndexPath:[NSIndexPath
                                                                                                         indexPathForItem:0
                                                                                                         inSection:0]];
        [attributesArray addObject:attributes];
      }
    
      return attributesArray;
    }
    
    0 讨论(0)
提交回复
热议问题