TableHeaderView 使用AutoLayout自适应高度

主要点
  1. 设置headView宽度约束等于tableView的宽度,并将headView赋值给tableView.tableHeaderView
  2. 布局子视图,且让headView能够根据子视图的约束,自动计算出自身的高度
  3. 刷新视图布局,并重新将headView赋值给tableView.tableHeaderView

上代码:

- (void)setupTableHeadView {
    UIView *headView = [[UIView alloc] init];
    self.tableView.tableHeaderView = headView;
    // 此处必须指定宽度
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(YD_SCREEN_SIZE.width);
        make.top.mas_equalTo(self.tableView.mas_top);
    }];

    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor yd_color_212639];
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 4;
    [headView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headView.mas_top).offset(30);
        make.left.equalTo(headView.mas_left).offset(10);
        make.right.equalTo(headView.mas_right).offset(-10);
        make.bottom.equalTo(headView.mas_bottom);
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"开启报警提醒";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:16];
    [bgView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView.mas_top);
        make.left.equalTo(bgView.mas_left).offset(15);
        make.height.mas_equalTo(100);
    }];
    [label setContentHuggingPriority:UILayoutPriorityDefaultLow - 1 forAxis:UILayoutConstraintAxisHorizontal];
//    [label setContentCompressionResistancePriority:UILayoutPriorityDefaultLow - 1 forAxis:UILayoutConstraintAxisHorizontal];

    // 开关
    _switchButton = [[UIButton alloc] init];
    [_switchButton setImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal];
    [bgView addSubview:_switchButton];
    [_switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.mas_right);
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.centerY.equalTo(label.mas_centerY);
    }];

    // 分割线
    UIView *splitView = [[UIView alloc] init];
    splitView.backgroundColor = [UIColor yd_color_main];
    [bgView addSubview:splitView];
    [splitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.top.equalTo(label.mas_bottom);
        make.left.equalTo(bgView.mas_left).offset(10);
        make.right.equalTo(bgView.mas_right).offset(-10);
    }];
    
    // 说明
    UILabel *descriptionLabel = [[UILabel alloc] init];
    descriptionLabel.text = @"开启后将在设置的时间段内接收到三级报警声";
    descriptionLabel.font = [UIFont systemFontOfSize:10];
    descriptionLabel.textColor = [UIColor yd_color_656977];
    [bgView addSubview:descriptionLabel];
    [descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(splitView.mas_bottom);
        make.left.equalTo(bgView.mas_left).offset(10);
        make.bottom.equalTo(bgView.mas_bottom);
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.height.mas_equalTo(40);
    }];
    
    // iOS 10(含)之后需调用该方法,否则tableHeaderView会盖着cell
    [self.tableView.tableHeaderView layoutIfNeeded]; // 或者[self.tableView layoutIfNeeded];
    // 若系统版本为iOS9,还必须在取得正确的frame之后,重新设置tableHeaderView
    // iOS10(含),可省略该操作
    self.tableView.tableHeaderView = headView;
}

效果图如下:


TableHeaderView 使用AutoLayout自适应高度_第1张图片
效果图

你可能感兴趣的:(TableHeaderView 使用AutoLayout自适应高度)