autolayout约束设置完全后 可获取容器View高度
2019-09-26 本文已影响0人
wustzhy
测试代码
//Auto Layout能够计算出正确的界面的必要条件是约束充分
//创建label
self.headerLable = [UILabel new];
self.headerLable.numberOfLines = 0;
//创建headerView
self.headerView = [UIView new];
[self.headerView addSubview:self.headerLable];
self.tableView.tableHeaderView = self.headerView;
//设置约束
[self.headerLable mas_makeConstraints:^(MASConstraintMaker *make) {
// make.edges.mas_equalTo(UIEdgeInsetsMake(10, 10, 10, 10)); // 这样设置约束,无法正确显示、获取容器view高度
make.left.top.mas_equalTo(10);
make.width.mas_lessThanOrEqualTo(150);
make.bottom.mas_lessThanOrEqualTo(-10);
}];
// [self.headerLable setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
//
// {
// UILabel *label = [UILabel new];
// label.numberOfLines = 0;
// label.backgroundColor = [UIColor redColor];
// [self.headerView addSubview:label];
// //UITableView 不能给 tableFooterView添加约束,但可给其子view添加约束
// [label mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.mas_equalTo(self.headerLable.mas_right).offset(20);
// make.right.mas_lessThanOrEqualTo(-10);
// make.top.mas_equalTo(12);
// make.bottom.mas_equalTo(-10);
// }];
// label.text = @"测试文本测试文本测试文本测试文本测试文本测试文本测试文本测试文本测试文本测试文本";
//// label.preferredMaxLayoutWidth = CGRectGetWidth([UIScreen mainScreen].bounds) - 20;
// }
//设置文本
self.headerLable.text = @"测试文本测试文本测试文本测试文本测试文本测试文本测试文本测试文本测试文本测试文本";
// self.headerLable.preferredMaxLayoutWidth = CGRectGetWidth([UIScreen mainScreen].bounds) - 20;
CGSize size = [self.headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
self.headerView.frame = CGRectMake(0, 0, size.width, size.height);
UIView *tableFooter = [UIView new];
self.tableView.tableFooterView = tableFooter;
// 以上 perfect展示, 以及单行两label设置好抗压缩约束,同样可以获取真实宽高
// --------------------------------------------
UILabel *footerLabel = [UILabel new];
footerLabel.numberOfLines = 0;
footerLabel.backgroundColor = [UIColor redColor];
[tableFooter addSubview:footerLabel];
//UITableView 不能给 tableFooterView添加约束,但可给其子view添加约束
[footerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(tableFooter).mas_offset([NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)]);
}];
// footerLabel.preferredMaxLayoutWidth = CGRectGetWidth([UIScreen mainScreen].bounds) - 20;
footerLabel.text = @"[footerLabel mas_makeConstraints:^(MASConstraintMaker *make) {\
make.edges.mas_equalTo(tableFooter).mas_offset([NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)]);\
}];"; //计算tableFooterView的size
// size = [tableFooter systemLayoutSizeFittingSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 1000)]; // 这个结合preferredMaxLayoutWidth使用才行....
size = [tableFooter systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
tableFooter.frame = CGRectMake(0, 0, size.width, size.height);
self.tableView.tableFooterView = tableFooter;
总结
- 方法一
// 这样设置约束,无法正确显示、获取容器view高度
make.edges.mas_equalTo(UIEdgeInsetsMake(10, 10, 10, 10));
// 须结合使用
label.preferredMaxLayoutWidth = CGRectGetWidth([UIScreen mainScreen].bounds) - 20;
- 方法二
不使用edges
设置约束,使用笨拙方式👇
[self.headerLable mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.mas_equalTo(10);
make.width.mas_lessThanOrEqualTo(150);
make.bottom.mas_lessThanOrEqualTo(-10);
}];
方法二的好处是,一行有多个label时,设置好抗压缩约束,依然能获取正确高度
注意
make.width.mas_lessThanOrEqualTo(150);
别写成了
make.right.mas_lessThanOrEqualTo(-10);
有人知道问什么吗? 留言告诉我一下哈 ^_~