使用Masonry设置自适应高度的UITableviewHeadView

首先是懒加载部分

- (UIView *)headView
{
    if (!_headView) {
        _headView = [[UIView alloc]init];
        _headView.backgroundColor = HEXColor(0xf2f2f2);

        _headBtn = [[UIButton alloc]init];
        _headBtn.layer.cornerRadius = 25;
        [_headBtn setBackgroundImage:[UIImage imageNamed:@"livePortraitImage"] forState:0];
        _headBtn.layer.masksToBounds = YES;
        [_headView addSubview:_headBtn];
        [_headBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(15);
            make.left.mas_equalTo(13);
            make.width.mas_equalTo(50);
            make.height.mas_equalTo(50);
        }];

        //时间
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.font = [UIFont systemFontOfSize:11];
        _timeLabel.textColor = HEXColor(0x999999);
        _timeLabel.text = @"2小时前";
        [_headView addSubview:_timeLabel];
        [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_headView);
            make.top.equalTo(_headView).offset(23);
            make.width.mas_equalTo(55);
            make.height.mas_equalTo(12);
        }];

        //昵称
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = [UIFont systemFontOfSize:13];
        _nameLabel.text =@"蔡卓妍";
        _nameLabel.textColor = HEXColor(0x666666);
        [_headView addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_headBtn.mas_right).offset(6);
            make.top.equalTo(_timeLabel);
            make.right.equalTo(_timeLabel.mas_left);
            make.height.mas_equalTo(14);
        }];

        //内容
        _contentLabel = [[UILabel alloc]init];
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.textColor = HEXColor(0x333333);
        _contentLabel.numberOfLines = 0;
        _contentLabel.text = @"轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝轩墨宝宝";
        [_headView addSubview:_contentLabel];
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_nameLabel);
            make.top.equalTo(_nameLabel.mas_bottom).offset(9);
            make.right.equalTo(_timeLabel.mas_left);
        }];

        [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(kScreenWidth);
            make.bottom.equalTo(_contentLabel).offset(5);
        }];
    }
    return _headView;
}

添加部分(计算出自适应高度 重新赋值)

UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    [headView addSubview:self.headView];

    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(headView);
    }];

    CGFloat height =[headView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    CGRect frame = headView.frame;
    frame.size.height = height;
    headView.frame = frame;
    self.mytable.tableHeaderView = headView;

你可能感兴趣的:(ios)