在 iOS 7 中更新约束后获取"Unable to simultaneously satisfy constraints"



我有一个UITableViewController,有一个自定义视图,里面有一些子视图和控件,在表的头部。这些子视图中的每一个都是自定义子视图(使用自定义子视图用IBInspectable绘制角半径),每个子视图都有顶部,底部,前导和尾随空间的约束(全部设置为8)以及高度(设置为60,80或100,取决于每个子视图)。

这些子视图约束之一可以在运行时根据用户交互以编程方式修改。为此,我创建了这两个方法:

- (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];
}

因为所有这些子视图都在表头视图中,每次我改变约束时,头应该根据情况展开或压缩,这就是为什么我使用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)。这就是你收到这条信息的原因。

你要么调整约束,让它们加起来等于父视图的高度,要么调整父视图的大小,让它的大小与子视图的大小相匹配。

你不需要现在所有的约束。由于您为所有子视图和间距约束设置了特定的高度,因此您只需要将它们锚定在父视图的顶部或底部。

编辑:

你不需要这两个约束:V:|-(8)-[SeachFormBackgroundView:0x7bea3ab0]V:[SeachFormBackgroundView:0x7bf54180]-(8)-|。第一个将子视图锚定在父视图的顶部,第二个将子视图锚定在父视图的底部。删除其中一个(我希望是底部的那个),视图将落在适当的位置,而不会抛出任何AutoLayout异常。

设置header视图时,表视图读取其大小并为该高度设置约束。更新头部视图本身的约束不会导致高度的重新计算。你应该删除标题视图,更新它的约束,布局它,然后再次添加标题视图。

相关内容

最新更新