简体   繁体   English

如何在顶部UICollectionView上制作粘性标头

[英]How to make a sticky header on top UICollectionView

Currently I have a header cell in my UICollectionView. 目前,我的UICollectionView中有一个标头单元格。 When I try to scroll UICollectionView, header cell will scroll together with UICollectionView list. 当我尝试滚动UICollectionView时,标题单元格将与UICollectionView列表一起滚动。 May I know how to set the header cell stick on top? 我可以知道如何将顶部单元格放在顶部吗? Please help. 请帮忙。 Thank you. 谢谢。

My current result:- 我目前的结果: 在此处输入图片说明

My expected result:- 我的预期结果: 在此处输入图片说明

Here is my sample code for header cell in my ViewController:- 这是我的ViewController中的标头单元格的示例代码:-

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        DCHoverFlowLayout *layout = [DCHoverFlowLayout new];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.frame = CGRectMake(0, DCTopNavH, ScreenW, ScreenH - DCTopNavH);

        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;

        [_collectionView registerClass:[Product_HeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:Product_HeadViewID];

    }
    return _collectionView;
}



- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader){

        Product_HeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:Product_HeadViewID forIndexPath:indexPath];
        WEAKSELF

  };
        reusableview = headerView;
        return cell;
    }

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    _lastContentOffset = scrollView.contentOffset.y;

}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{

    if(scrollView.contentOffset.y > _lastContentOffset){
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        self.collectionView.frame = CGRectMake(0, 20, ScreenW, ScreenH - 20);

        self.view.backgroundColor = [UIColor whiteColor];
    }else{
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        self.collectionView.frame = CGRectMake(0, DCTopNavH, ScreenW, ScreenH - DCTopNavH);

        self.view.backgroundColor = ThemeBackgroundColor;
    }
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //Detect Button Visible
    _backTopButton.hidden = (scrollView.contentOffset.y > ScreenH) ? NO : YES;


    WEAKSELF
    [UIView animateWithDuration:0.25 animations:^{
        __strong typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.footprintButton.dc_y = (strongSelf.backTopButton.hidden == YES) ? ScreenH - 60 : ScreenH - 110;
    }];

}

UPDATED:- 更新:-

After applied Ted's code, here is the result and apps will crash. 应用Ted的代码后,将得到结果,应用将崩溃。 在此处输入图片说明

One option is to use a UIViewController instead of a UICollectionViewController . 一种选择是使用UIViewController而不是UICollectionViewController Add the view to use as your header to the view controller and then add a collection view below that header view. 将视图用作标题添加到视图控制器,然后在该标题视图下添加一个集合视图。

This way the header is not part of the collection view and it won't scroll at all. 这样,标题就不会成为集合视图的一部分,并且根本不会滚动。

In your viewForSupplementaryElementOfKind method. 在您的viewForSupplementaryElementOfKind方法中。 Add these line: 添加以下行:

UICollectionViewFlowLayout *layout = collectionView.collectionViewLayout;
layout.sectionHeadersPinToVisibleBounds = YES;

Swift version of tedd 's answer: Swift版本的tedd的答案:

let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionHeadersPinToVisibleBounds = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM