【发布时间】:2015-11-10 03:22:18
【问题描述】:
我使用砖石来布局我的 UI。
在我的viewController中,我在tableView中添加了一个headView,在headView中,添加了一个按钮。
当我向按钮添加右约束时,它在下面出现警告/错误:
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:0x7ff5fa9ead00 UIButton:0x7ff5fa9e4ae0.left == UIView:0x7ff5fa9dd780.left + 20>",
"<MASLayoutConstraint:0x7ff5fa9eae80 UIButton:0x7ff5fa9e4ae0.right == UIView:0x7ff5fa9dd780.right - 20>",
"<NSLayoutConstraint:0x7ff5fad3ac10 UIView:0x7ff5fa9dd780.width ==>"
)
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x7ff5fa9eae80 UIButton:0x7ff5fa9e4ae0.right == UIView:0x7ff5fa9dd780.right - 20>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
我的代码如下:
-(void)viewDidLoad{
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.showsHorizontalScrollIndicator = NO;
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tableView];
UIView * headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 438)];
headView.backgroundColor = [UIColor whiteColor];
self.headView = headView;
self.tableView.tableHeaderView = headView;
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.headView addSubview:button];
[button setTitle:@"a button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(clickThawButton:) forControlEvents:UIControlEventTouchUpInside];
button.clipsToBounds = YES;
button.layer.cornerRadius = 5.0f;
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(@20);
//make.right.equalTo(@-20); // The error line
make.width.equalTo(@(MainScreenWidth - 40)); //I slove the problem by this.
make.top.equalTo(otherView.mas_bottom).offset(25);
make.height.equalTo(@55);
}];
}
虽然我通过约束宽度解决了这个问题,但我想知道为什么会发生这种情况。或者有什么建议吗?
【问题讨论】:
-
同时设置左右和宽度约束是否会出错?还是在设置左右约束时出错?
-
未设置在一起。我设置了左右,它得到了错误。我设置了 left,width 并且效果很好。
-
根据日志消息,
<NSLayoutConstraint:0x7ff5fad3ac10 UIView:0x7ff5fa9dd780.width ==>与right space with button冲突,因为按钮大小不能为负数(小于零)。不知何故,button的超级视图的宽度设置为低于 20,这使得按钮的宽度为负。尝试将 tableView 的 frameWidth 设置为 20 以上。
标签: ios objective-c autolayout masonry