水平滚动方向模式下的 UICollectionView 标题位置与流布局 [英] UICollectionView header position in horizontal scroll direction mode with flow layout

查看:19
本文介绍了水平滚动方向模式下的 UICollectionView 标题位置与流布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 header on the top of section cells

你知道我该怎么做吗?

推荐答案

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

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

设置它(可能在 viewDidLoad 中):

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;
}

这篇关于水平滚动方向模式下的 UICollectionView 标题位置与流布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆