Autolayout layoutIfNeeded打破了约束

时间:2016-08-09 14:40:18

标签: ios objective-c autolayout storyboard xib

我在xib文件中创建了以下视图。enter image description here有两个约束,我一次想要其中一个,所以我激活一个并停用其他约束。

其中一个约束是View(顶层视图)view.bottom == inputView.bottom。当文本视图中的用户输入正确时,将启用此约束。如果用户输入了一些特殊字符,则会显示错误消息并停用此约束,并激活另一个view.bottom == errorLabel.bottom约束。最初这两个约束都存在于xib文件中,但view.bottom == errorLabel.bottom设置为750优先级,view.bottom == input View.bottom设置为所需优先级1000。这是为了避免冲突的约束。

现在,当用户输入一些输入时,我会这样做。

[NSLayoutConstraint deactivateConstraints:@[self.inputViewConstraint, self.inputViewWithErrorConstraint]];

        self.inputViewConstraint.active = (result == NBValidationResultOK);
        self.inputViewWithErrorConstraint.active = !self.inputViewConstraint.active;

        [UIView animateWithDuration:0.1 animations:^{
            [self layoutIfNeeded];
        }];


inputViewConstraint -----> view.bottom == InputView.bottom
inputViewWithErrorConstraint -----> view.bottom == errorLabel.bottom

当我执行上面的代码时,调试器会给我打破约束警告。

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-08-09 17:11:45.181 NB[17707:1811620] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7faf12ac8850 h=-&- v=-&- UIView:0x7faf12ac41f0.width == NBSendMoneyTextInputView:0x7faf128c8ab0.width>",
    "<NSLayoutConstraint:0x7faf12a74020 H:|-(16)-[NBUISecondaryLabel:0x7faf12ac3560'SecondaryLabel']   (Names: '|':UIView:0x7faf12ac31e0 )>",
    "<NSLayoutConstraint:0x7faf12a4a950 UITextView:0x7faf140b7400.leading == NBUISecondaryLabel:0x7faf12ac3560'SecondaryLabel'.leading>",
    "<NSLayoutConstraint:0x7faf12a76630 H:[UITextView:0x7faf140b7400]-(16)-|   (Names: '|':UIView:0x7faf12ac31e0 )>",
    "<NSLayoutConstraint:0x7faf12a2ae80 H:[UIView:0x7faf12ac31e0]-(0)-|   (Names: '|':UIView:0x7faf12ac41f0 )>",
    "<NSLayoutConstraint:0x7faf12a2aed0 H:|-(0)-[UIView:0x7faf12ac31e0]   (Names: '|':UIView:0x7faf12ac41f0 )>",
    "<NSLayoutConstraint:0x7faf12ac5ac0 '_UITemporaryLayoutWidth' H:[NBSendMoneyTextInputView:0x7faf128c8ab0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7faf12a4a950 UITextView:0x7faf140b7400.leading == NBUISecondaryLabel:0x7faf12ac3560'SecondaryLabel'.leading>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-08-09 17:11:45.182 NB[17707:1811620] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7faf12ac88f0 h=-&- v=-&- UIView:0x7faf12ac41f0.height == NBSendMoneyTextInputView:0x7faf128c8ab0.height>",
    "<NSLayoutConstraint:0x7faf12aa8b20 V:[NBUISeparatorView:0x7faf12ac3990(2)]>",
    "<NSLayoutConstraint:0x7faf12a64b10 V:|-(18)-[NBUISecondaryLabel:0x7faf12ac3560'SecondaryLabel']   (Names: '|':UIView:0x7faf12ac31e0 )>",
    "<NSLayoutConstraint:0x7faf12a368b0 V:[NBUISecondaryLabel:0x7faf12ac3560'SecondaryLabel']-(4)-[UITextView:0x7faf140b7400]>",
    "<NSLayoutConstraint:0x7faf12a41f60 V:[UITextView:0x7faf140b7400]-(2)-[NBUISeparatorView:0x7faf12ac3990]>",
    "<NSLayoutConstraint:0x7faf12a3e240 V:[NBUISeparatorView:0x7faf12ac3990]-(0)-|   (Names: '|':UIView:0x7faf12ac31e0 )>",
    "<NSLayoutConstraint:0x7faf12a211e0 V:|-(0)-[UIView:0x7faf12ac31e0]   (Names: '|':UIView:0x7faf12ac41f0 )>",
    "<NSLayoutConstraint:0x7faf12a9ba30 V:[UIView:0x7faf12ac31e0]-(0)-|   (Names: '|':UIView:0x7faf12ac41f0 )>",
    "<NSLayoutConstraint:0x7faf12ac5bf0 '_UITemporaryLayoutHeight' V:[NBSendMoneyTextInputView:0x7faf128c8ab0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7faf12aa8b20 V:[NBUISeparatorView:0x7faf12ac3990(2)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-08-09 17:11:45.196 NB[17707:1811620] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7faf12ac88f0 h=-&- v=-&- UIView:0x7faf12ac41f0.height == NBSendMoneyTextInputView:0x7faf128c8ab0.height>",
    "<NSLayoutConstraint:0x7faf12a64b10 V:|-(18)-[NBUISecondaryLabel:0x7faf12ac3560'SecondaryLabel']   (Names: '|':UIView:0x7faf12ac31e0 )>",
    "<NSLayoutConstraint:0x7faf12a368b0 V:[NBUISecondaryLabel:0x7faf12ac3560'SecondaryLabel']-(4)-[UITextView:0x7faf140b7400]>",
    "<NSLayoutConstraint:0x7faf12a41f60 V:[UITextView:0x7faf140b7400]-(2)-[NBUISeparatorView:0x7faf12ac3990]>",
    "<NSLayoutConstraint:0x7faf12a3e240 V:[NBUISeparatorView:0x7faf12ac3990]-(0)-|   (Names: '|':UIView:0x7faf12ac31e0 )>",
    "<NSLayoutConstraint:0x7faf12a211e0 V:|-(0)-[UIView:0x7faf12ac31e0]   (Names: '|':UIView:0x7faf12ac41f0 )>",
    "<NSLayoutConstraint:0x7faf12a9ba30 V:[UIView:0x7faf12ac31e0]-(0)-|   (Names: '|':UIView:0x7faf12ac41f0 )>",
    "<NSLayoutConstraint:0x7faf12ac5bf0 '_UITemporaryLayoutHeight' V:[NBSendMoneyTextInputView:0x7faf128c8ab0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7faf12a368b0 V:[NBUISecondaryLabel:0x7faf12ac3560'SecondaryLabel']-(4)-[UITextView:0x7faf140b7400]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

请注意,只有在我调用layoutIfNeeded()方法时才会显示这些警告。

在我的公开场合,没有相互矛盾的约束。但如果有人可以帮助我,那将是非常高兴的。

2 个答案:

答案 0 :(得分:0)

作为建议:

您可以将其中一个约束更改为低优先级,

之前约束活动状态发生了变化:

enter image description here

在您的情况下,低优先级约束应该是self.inputViewConstraintself.inputViewWithErrorConstraint(不是两者)。

答案 1 :(得分:0)

在打开某些约束之前,必须将活动标志设置为false。例如:

http://sfd98sdf.ngrok.io

如果您同时打开两个约束,您将收到警告。