UICollectionView header position in horizontal scroll direction mode with flow layout

核能气质少年 提交于 2019-12-18 18:48:50

问题


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

Have you any idea how can I do this?


回答1:


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



回答2:


I solved the problem using DateFlowLayout.

See how I configured it in this other answer.



来源:https://stackoverflow.com/questions/14579535/uicollectionview-header-position-in-horizontal-scroll-direction-mode-with-flow-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!