【问题标题】:How to make a sticky header on top UICollectionView如何在顶部 UICollectionView 上制作粘性标题
【发布时间】:2018-04-26 02:02:13
【问题描述】:

目前我的 UICollectionView 中有一个标题单元格。当我尝试滚动 UICollectionView 时,标题单元格将与 UICollectionView 列表一起滚动。 我可以知道如何将标题单元格设置在顶部吗?请帮忙。谢谢。

我目前的结果:-

我的预期结果:-

这是我的 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;
    }];

}

更新:-

应用 Ted 的代码后,结果如下,应用程序将崩溃。

【问题讨论】:

    标签: ios objective-c header collectionview


    【解决方案1】:

    tedd 的 Swift 版本的回答:

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

    【讨论】:

    • 哇,这解决了我在 iOS 12 上的问题。iOS 13 已经运行良好。如此随意的苹果,如此随意。
    【解决方案2】:

    在您的 viewForSupplementaryElementOfKind 方法中。添加这些行:

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

    【讨论】:

    • 嗨,泰德,我在应用您的代码时遇到了这个错误:- 使用“UICollectionViewLayout *”类型的表达式初始化“UICollectionViewFlowLayout *”的指针类型不兼容。有什么想法吗?
    • 你试过运行它吗?就我而言,这只是一个警告。
    • 嗨泰德,请参阅我的截图。应用后应用会崩溃
    • 崩溃信息是什么?
    • 索引路径 ( {length = 2, path = 0 - 0}) 的补充项目的布局属性从 更改索引路径: ( {长度 = 2,路径 = 0 - 0});元素种类:(UICollectionElementKindSectionHeader);帧 = (0 12; 414 40); zIndex = 10;到 索引路径: ( {length = 2, path = 0 - 0});元素种类:(UICollectionElementKindSectionHeader);帧 = (0 12; 414 40); zIndex = 7;不使布局无效'
    【解决方案3】:

    一种选择是使用UIViewController 而不是UICollectionViewController。将要用作标题的视图添加到视图控制器,然后在该标题视图下方添加一个集合视图。

    这样标题就不是集合视图的一部分,它根本不会滚动。

    【讨论】:

    • 嗨 rmaddy,目前我在标题单元格中使用 @interface Product_HeadView : UICollectionReusableView。你的意思是改变这个?
    猜你喜欢
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 2014-01-09
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2011-12-13
    相关资源
    最近更新 更多