【问题标题】:Change section header view after scrolling down向下滚动后更改部分标题视图
【发布时间】:2014-01-02 05:15:22
【问题描述】:

我想在用户向下滚动时修改部分标题视图,类似于音乐应用中的内容 (注意视图背景颜色如何变化并获得底部边框)

有没有一种很好的方法来跟踪视图何时位于部分顶部或处于滚动位置?

更新:

到目前为止,我唯一的解决方案是保留所有节标题视图的数组并更改 scrollViewDidScroll 中第一个可见节的视图:委托方法(使用 tableView.indexPathsForVisibleRows 数组获取第一个可见节索引)

如果有人能想出一个更简单的方法,那就太好了!

【问题讨论】:

    标签: ios objective-c cocoa-touch ios7


    【解决方案1】:

    您可以在 scrollViewDidScroll 方法中修改节标题视图的颜色(以及您想要的任何其他颜色)。此示例在用户向下滚动时使浮动标题视图的颜色变暗,并将该颜色的白色值保持在 0.9 和 0.6 之间。如果您向下滚动超过 5 个点,它还会取消隐藏标题视图中的底部边框线。

    RDHeaderView 的 .m 文件:

    - (id)init{
        self = [super init];
        if (self) {
            UIView *line = [[UIView alloc] init];
            [line setTranslatesAutoresizingMaskIntoConstraints:NO];
            line.backgroundColor = [UIColor darkGrayColor];
            [self addSubview:line];
            [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[line]|" options:0 metrics:nil views:@{@"line":line}]];
            [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[line]|" options:0 metrics:nil views:@{@"line":line}]];
            [line addConstraint:[NSLayoutConstraint constraintWithItem:line attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:1]];
            self.bottomLine = line;
            self.bottomLine.hidden = YES;
        }
        return self;
    }
    

    表格视图控制器中的相关方法:

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        RDHeaderView *header = [[RDHeaderView alloc] init];
        header.contentView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        return header;
    }
    
    
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView {
        NSInteger topSection = [[self.tableView indexPathsForVisibleRows].firstObject section];
        NSInteger sectionYOffset = [self.tableView rectForHeaderInSection:topSection].origin.y;
        RDHeaderView *pinnedHeader = (RDHeaderView *)[self.tableView headerViewForSection:topSection];
        pinnedHeader.bottomLine.hidden = ((scrollView.contentOffset.y - sectionYOffset) > 5)? NO: YES;
        CGFloat colorOffset = fmaxf(0.6, 0.9 - (scrollView.contentOffset.y - sectionYOffset)/1000.0);
        if (colorOffset > 0.9) colorOffset = 0.9;
        pinnedHeader.contentView.backgroundColor = [UIColor colorWithWhite:colorOffset alpha:1];
    }
    
    
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        return 80;
    }
    

    【讨论】:

    • 抱歉,这会改变所有的节标题视图,你知道我在表格视图中有不止一个节。想到了一个类似的解决方案,我跟踪所有部分位置并使用滚动视图委托方法来相应地更改视图。我正在寻找动态解决方案(如果有的话)
    • @EdLess,我已经更改了我的答案,以展示我是如何为多个部分完成的。
    【解决方案2】:

    也许存在更简单的解决方案,但这将是实现您想要的一种方式:

    • 删除 tableHeader,并将其作为单独的子视图添加到您的 viewControllers 视图(注意不要使用 uitableviewController,因为该 viewController 将 tableview 作为其视图,而您不希望这样)

    • 将 tableView 向下移动,使其正好位于之前的 tableheader 视图下方

    • 计算并设置 tableView 的 contentOffset(它是一个 UIScrollView),以使单元格似乎不会跳转到新位置。

    另外,您可以尝试返回 sectionHeaderView(它会随着 tableView 向下滚动,但不会向上滚动,例如,请参阅联系人列表中的工作原理)。将该视图用作单元格或节标题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      • 2019-06-01
      • 2017-07-14
      • 1970-01-01
      相关资源
      最近更新 更多