tableHeaderView的Masonry

tableHeaderView的Masonry

    UIView *headView =[UIView new];
    headView.backgroundColor = [UIColor yellowColor];

    UIView *view1 =[UIView new];
    view1.backgroundColor =[UIColor redColor];
    [headView addSubview:view1];
    [view1 makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(10);
        make.right.equalTo(-10);
        make.height.equalTo(100);
    }];

    UIView *view2 =[UIView new];
    view2.backgroundColor =[UIColor blackColor];

    UILabel *label = [[UILabel alloc] init];
    [label setNumberOfLines:0];
    // 计算UILabel的preferredMaxLayoutWidth值,多行时必须设置这个值,否则系统无法决定Label的宽度(*****非常重要*****)
    label.preferredMaxLayoutWidth = ULMainScreenWidth - 20;
    [label setBackgroundColor:[UIColor greenColor]];
    [label setText:@"这样写了之后在7.1里面直接报错Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super. 在8里面没有报错,但是headView并没有显示,按照字面的意思理解就是父视图要重新布局,我觉得应该是tableHeaderView在tableview里面系统应该有自己的布局,在外面设置的话就冲突了,然后报错,也不知道这个理解是不是对的。\n后来stackoverflow上面看了一些问答,基本上是先设置好headView,根据headView的Subview的autolayout计算出高度,然后再高度重新赋值给headView。下面是headView上面有2个子视图,高度各100"];

    [view2 addSubview:label];
    [label makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(0);
        make.bottom.equalTo(view2);
    }];

    [headView addSubview:view2];
    [view2 makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view1.bottom).offset(10);
        make.left.right.equalTo(view1);
        make.bottom.equalTo(headView).offset(-10);
    }];

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

来自:http://qcliwei.com/2015/08/02/tableHeaderView-autolayout/

http://tutuge.me/2015/08/08/autolayout-example-with-masonry2/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值