使用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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值