具有流布局的水平滚动方向模式下的UICollectionView标题位置

时间:2022-09-06 20:24:56

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

我有带有水平滚动方向的Flow布局的ios UICollectionView。在这种情况下,典型的标题位置位于单元格的左侧。

I want to make header on the top of section cells

我想在节细胞的顶部制作标题

Have you any idea how can I do this?

你知道我怎么能这样做吗?

2 个解决方案

#1


-6  

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

我认为您可以使用标头的标准行为获得所需的内容。

Set it up (probably in viewDidLoad):

设置它(可能在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;
}

#2


5  

I solved the problem using DateFlowLayout.

我使用DateFlowLayout解决了这个问题。

See how I configured it in this other answer.

看看我在另一个答案中如何配置它。

#1


-6  

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

我认为您可以使用标头的标准行为获得所需的内容。

Set it up (probably in viewDidLoad):

设置它(可能在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;
}

#2


5  

I solved the problem using DateFlowLayout.

我使用DateFlowLayout解决了这个问题。

See how I configured it in this other answer.

看看我在另一个答案中如何配置它。