美文网首页
tableHeadVeiw如何使用Masonry

tableHeadVeiw如何使用Masonry

作者: Zhui_Do | 来源:发表于2017-09-26 17:12 被阅读39次
    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

相关文章

网友评论

      本文标题:tableHeadVeiw如何使用Masonry

      本文链接:https://www.haomeiwen.com/subject/kvxeextx.html