当我激活/拒绝约束时如何修复NSLayoutConstraint警告?

时间:2016-10-27 08:02:52

标签: nslayoutconstraint ios-autolayout

我在故事板中有一个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>"
)

如何修复警告?

1 个答案:

答案 0 :(得分:1)

尝试后,我发现了主动约束,然后是主动约束,然后更新其他约束可以修复警告。

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