Chris Yim
Chris Yim

Reputation: 1392

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

I have a titleView in a storyboard. It's height should be adjusted by title. So i write code like this:

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

It works, but has some warnings:

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>"
)

How to fix the warnings?

Upvotes: 2

Views: 322

Answers (1)

Chris Yim
Chris Yim

Reputation: 1392

After trying, i find deactive constraints, then active constraints, then update other constraints can fix the warnings.

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

Upvotes: 2

Related Questions