我有一个 UITableViewController,它有一个自定义 View ,里面有一些 subview 和控件,位于表的标题中。这些 subview 中的每一个都是自定义 subview (使用自定义 subview 通过 IBInspectable 绘制圆角半径)并且每个 subview 都有顶部、底部、前导和尾随空间(全部设置为 8)以及高度(设置为 60, 80 或 100,具体取决于每个 subview )。
这些 subview 约束之一可以在运行时根据用户交互以编程方式修改。为此,我创建了这两种方法:
- (void)showSearchTypeTermField:(BOOL)animated
{
self.searchTypeHeightConstraint.identifier = @"Height 100";
[self.searchTypeHeightConstraint setConstant:100.0];
if (animated) {
[self.tableView.tableHeaderView layoutIfNeeded];
[UIView animateWithDuration:0.3 animations:^{
[self.tableView.tableHeaderView layoutIfNeeded];
}];
} else {
[self.tableView.tableHeaderView layoutIfNeeded];
}
self.searchTypeTermField.hidden = NO;
self.searchTypeTermField.text = @"";
[self.tableView.tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize];
}
- (void)hideSearchTypeTermField:(BOOL)animated
{
self.searchTypeHeightConstraint.identifier = @"Height 60";
[self.tableView.tableHeaderView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[self.searchTypeHeightConstraint setConstant:60.0];
if (animated) {
[self.tableView.tableHeaderView layoutIfNeeded];
[UIView animateWithDuration:0.3 animations:^{
[self.tableView.tableHeaderView layoutIfNeeded];
}];
} else {
[self.tableView.tableHeaderView layoutIfNeeded];
}
self.searchTypeTermField.hidden = YES;
[self.tableView.tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
}
因为所有这些 subview 都在表头 View 中,所以每次我更改约束时,表头都应该根据情况扩展或压缩,这就是我使用 self.tableView.tableHeaderView systemLayoutSizeFittingSize
的原因。
问题是,当我第一次在 viewdidLoad
上调用 [self hideSearchTypeTermField:NO]
时,我遇到了这个错误,尽管在视觉上一切似乎都很好:
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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7bf504e0 V:[SeachFormBackgroundView:0x7bea3ab0(60)]>",
"<NSLayoutConstraint:0x7bf51760 V:[SeachFormBackgroundView:0x7bf50690(60)]>",
"<NSLayoutConstraint:0x7bf53390 'Height 60' V:[SeachFormBackgroundView:0x7bf518e0(60)]>",
"<NSLayoutConstraint:0x7bf53ec0 V:[SeachFormBackgroundView:0x7bf535a0(80)]>",
"<NSLayoutConstraint:0x7bf54a80 V:[SeachFormBackgroundView:0x7bf54180(80)]>",
"<NSLayoutConstraint:0x7bf54eb0 V:|-(8)-[SeachFormBackgroundView:0x7bea3ab0] (Names: '|':UIView:0x7bea3a10 )>",
"<NSLayoutConstraint:0x7bf54f40 V:[SeachFormBackgroundView:0x7bea3ab0]-(8)-[SeachFormBackgroundView:0x7bf50690]>",
"<NSLayoutConstraint:0x7bf54f70 V:[SeachFormBackgroundView:0x7bf50690]-(8)-[SeachFormBackgroundView:0x7bf518e0]>",
"<NSLayoutConstraint:0x7bf55090 V:[SeachFormBackgroundView:0x7bf518e0]-(8)-[SeachFormBackgroundView:0x7bf535a0]>",
"<NSLayoutConstraint:0x7bf550f0 V:[SeachFormBackgroundView:0x7bf54180]-(8)-| (Names: '|':UIView:0x7bea3a10 )>",
"<NSLayoutConstraint:0x7bf55180 V:[SeachFormBackgroundView:0x7bf535a0]-(8)-[SeachFormBackgroundView:0x7bf54180]>",
"<NSAutoresizingMaskLayoutConstraint:0x7c149fc0 h=--& v=--& V:[UIView:0x7bea3a10(428)]>"
)
我真的迷失了这个问题。我做错了什么?
谢谢。
最佳答案
如果您尝试为 SeachFormBackgroundView
实例添加高度(60 + 60 + 60 + 80 + 80 + 8 * 6 = 388 和它们的分隔符,您会发现它们不等于父级 UIView 高度 (428)。这就是您收到此消息的原因。
您必须调整约束,使它们加起来等于父 View 高度,或者调整父 View 的大小,使其大小与子约束相匹配。
您不需要现在拥有的所有限制。由于您为所有 subview 和间距约束设置了特定的高度,因此您只需要将它们锚定到父 View 的顶部或底部。
编辑:
您不需要这两个约束:V:|-(8)-[SeachFormBackgroundView:0x7bea3ab0]
和 V:[SeachFormBackgroundView:0x7bf54180]-(8)-|
。第一个将您的 subview 锚定到父 View 的顶部,第二个将它们锚定到您的父 View 的底部。删除其中一个(我希望是底部的那个), View 将就位而不会抛出任何 AutoLayout 异常。
关于ios - 在 iOS 7 中更新约束后获取 "Unable to simultaneously satisfy constraints",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32266352/