知乎日报-Home-HeaderViewInSection

注意蓝色的headerview的变化

1、自定义HeaderView

  • 忽略section为0的情况,使用另外的方法实现
  • 在tableView:heightForHeaderInSection: 设置了高度,在tableView:viewForHeaderInSection:,也可以不区分section为0的情况
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGFLOAT_MIN;
    }
    return kSectionHeaderHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    }
    SectionTitleView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([SectionTitleView class])];
    headerView.contentView.backgroundColor = kNavigationBarColor;
    headerView.textLabel.attributedText = [self.viewmodel titleForSection:section];
    return headerView;
}

2、自定义"今日新闻"的Header,实现section0的情况

  • 代码中的(60.f/255.f green:198.f/255.f blue:253.f/255.f)颜色为:
  • 创建一个仿NavBar的UIView,颜色如上图,完全透明不可见
  • 创建一个名字为"今日新闻"的UILabel,字体大小为18,高度为56
- (void)initSubViews {
    //创建其他View

    //官方版高度没有64,所以加个高度56仿冒NavBar的View,56也配合每个section(36)过渡时的动画
    UIView *navBarBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, kScreenWidth, 56.f)];
    navBarBackgroundView.backgroundColor = [UIColor colorWithRed:60.f/255.f green:198.f/255.f blue:253.f/255.f alpha:0.f];;
    [self.view addSubview:navBarBackgroundView];
    _navBarBackgroundView = navBarBackgroundView;

   UILabel *lab = [[UILabel alloc] init];
    lab.attributedText = [[NSAttributedString alloc] initWithString:@"今日新闻" attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18] ,NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [lab sizeToFit];
    [lab setCenter:CGPointMake(self.view.centerX, 38)];
    [self.view addSubview:lab];
    _newsTodayLb = lab;
}

3、自定义HeaderView

  • 当某个section0的Headerview可见时,设置navbar为56,_newsTodayLb为可见,section0没有HeadView,即实现"今日新闻的效果"
  • 当某个section0的HeaderView消失时,设置navbar为22,与自定义Headerview(高度为36)构成一个高度为56的仿NavBar
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if (section == 0) {
        [_navBarBackgroundView setHeight:56.f];
        _newsTodayLb.alpha = 1.f;
    }
}

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {
    if (section == 0) {
        [_navBarBackgroundView setHeight:20.f];
        _newsTodayLb.alpha = 0.f;
    }
}

4、滚动的时候,设置NarBar的可见度

  • -80 <= UITableView的滚动量 <= 0时,设置NarBar不可见,主要用于当滚动了几天的数据时候,重新滚回到顶部的时候,NarBar的透明度不是0
  • 0 < UITableView <= 300,根据偏移量,设置透明度,公式为:offsetY/220.f-56.f.
#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([scrollView isEqual:_mainTableView]) {
        CGFloat offSetY = scrollView.contentOffset.y;
        if (offSetY<=0&&offSetY>=-80) {
            //其他操作
            _navBarBackgroundView.backgroundColor = [UIColor colorWithRed:60.f/255.f green:198.f/255.f blue:253.f/255.f alpha:0.f];
        }else if(offSetY<-80){
            //其他操作
        }else if(offSetY <= 300) {
            //其他操作
            //navbar色彩不变, 透明的从0开始变化
            _navBarBackgroundView.backgroundColor = [UIColor colorWithRed:60.f/255.f green:198.f/255.f blue:253.f/255.f alpha:offSetY/(220.f-56.f)];
        }

        //其他操作
}
Copyright © 翻这个墙 2016 all right reserved,powered by Gitbook该文件修订时间: 2016-04-07 00:56:21