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

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

我想使标题在单元格的顶部

你有什么想法我该怎么做?

解决方法

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

设置(可能在viewDidLoad中):

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

然后回答标题视图:

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...