UICollectionView header position in horizontal scroll direction mode with flow layout

后端 未结 2 1185
无人及你
无人及你 2021-01-03 22:35

I have ios UICollectionView with Flow layout with horizontal scroll direction. In this situation typical header position is on the left side of cells.

I want to make

相关标签:
2条回答
  • 2021-01-03 23:09

    I think you can get what you need using standard behavior for the header.

    Set it up (probably in viewDidLoad):

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.bounds.size.width, 30);
    // other setup
    [self.collectionView setCollectionViewLayout:flowLayout];
    

    Then answer a header view:

    #define MY_HEADER_LABEL_TAG 128
    
    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                                UICollectionElementKindSectionHeader withReuseIdentifier:@"SectionHeader" forIndexPath:indexPath];
        UILabel *label = (UILabel *)[headerView viewWithTag:MY_HEADER_LABEL_TAG];
        if (!label) {
            label = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 5, 5)];
            label.tag = MY_HEADER_LABEL_TAG;
            label.font = [UIFont boldSystemFontOfSize:12];
            label.textColor = [UIColor darkGrayColor];
            [headerView addSubview:label];
        }
    
        label.text = [NSString stringWithFormat:@"Section %d", indexPath.section];
        return headerView;
    }
    
    0 讨论(0)
  • 2021-01-03 23:10

    I solved the problem using DateFlowLayout.

    See how I configured it in this other answer.

    0 讨论(0)
提交回复
热议问题