当我 active/deactive 约束时如何修复 NSLayoutConstraint 警告?

How to fix NSLayoutConstraint warnings when i active/deactive constrains?

我在故事板中有一个 titleView。它的高度应根据标题进行调整。所以我这样写代码:

- (void)adjustTitleView {
    if (self.actionTitle.length > 0) {
        self.titleViewHeight.active = NO;
        self.titleLabelTop.active = YES;
        self.titleLabelBottom.active = YES;
    } else {
        self.titleViewHeight.constant = 0.0;
        self.tableHeight.active = YES;
        self.titleLabelTop.active = NO;
        self.titleLabelBottom.active = NO;
    }
}

有效,但有一些警告:

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. 
(
    "<NSLayoutConstraint:0x1482f0540 UIView:0x1482f0320.height == 0.5>",
    "<NSLayoutConstraint:0x1482f0640 UIView:0x1482d0ad0.height == 0>",
    "<NSLayoutConstraint:0x1482f06e0 UIView:0x1482f0320.top == UILabel:0x1482d0c40.bottom + 6.5>",
    "<NSLayoutConstraint:0x1482f0780 UIView:0x1482d0ad0.bottom == UIView:0x1482f0320.bottom>",
    "<NSLayoutConstraint:0x1482f08c0 UILabel:0x1482d0c40.top == UIView:0x1482d0ad0.top + 8>"
)

如何修复警告?

尝试后,我找到了非活动约束,然后是活动约束,然后更新其他约束可以修复警告。

- (void)adjustTitleView {
    if (self.actionTitle.length > 0) {
        [NSLayoutConstraint deactivateConstraints:@[self.titleViewHeight]];
        [NSLayoutConstraint activateConstraints:@[self.titleLabelTop, self.titleLabelBottom]];
    } else {
        [NSLayoutConstraint deactivateConstraints:@[self.titleLabelTop, self.titleLabelBottom]];
        [NSLayoutConstraint activateConstraints:@[self.titleViewHeight]];
        self.titleViewHeight.constant = 0.0;
    }
}