这是代码:
- (void)viewDidLoad {
[super viewDidLoad];
.....
self.tableView.tableHeaderView = [self headerView];
//
/* If I addSubView instead of set to tableHeaderView, the constraint is OK. */
/* [self.view addSubview:[self headerView]]; */
//
}
- (UIView *)headerView {
UIView *headerView = [UIView new];
headerView.frame = CGRectMake(0, 0, 375, 270);
UIView *v1 = [UIView new];
[headerView addSubview:v1];
v1.backgroundColor = [UIColor redColor];
[v1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(headerView);
make.bottom.equalTo(headerView);
make.width.equalTo(@100);
make.height.equalTo(@50);
}];
UIView *v2 = [UIView new];
v2.backgroundColor = [UIColor blueColor];
[headerView addSubview:v2];
[v2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(v1.mas_right);
make.right.equalTo(headerView);
make.height.bottom.equalTo(v1);
}];
return headerView;
}
当我运行此代码时,模拟器显示正确的UI,但控制台显示:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<MASLayoutConstraint:0x7fc79143bf40 UIView:0x7fc7913992d0.left == UIView:0x7fc79136fa60.left>",
"<MASLayoutConstraint:0x7fc79143cc00 UIView:0x7fc7913992d0.width == 100>",
"<MASLayoutConstraint:0x7fc79143b8e0 UIView:0x7fc79140bd30.left == UIView:0x7fc7913992d0.right>",
"<MASLayoutConstraint:0x7fc79143ea70 UIView:0x7fc79140bd30.right == UIView:0x7fc79136fa60.right>",
"<NSLayoutConstraint:0x7fc790ffb950 UIView:0x7fc79136fa60.width == 0>"
)
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x7fc79143cc00 UIView:0x7fc7913992d0.width == 100>
代码有问题吗?或者tableHeaderView
和autoLayout
??