Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolayout layoutIfNeeded is breaking constraints

I am creating the following view in the xib file.enter image description here There are two constraint and I want one of them at a time so I am activating one and deactivating other constraint.

One of these constraint is View(toplevel view) view.bottom == inputView.bottom. This constraint is enabled when user input is correct in the text view. If user enters some special character then error message is shown and this constraint is deactivated and another constraint which is view.bottom == errorLabel.bottom is activated. Originally both of these constraints are there in the xib file but view.bottom == errorLabel.bottom is set 750 priority and view.bottom == input View.bottom is set with required priority 1000. This is to avoid conflicting constraints.

Now as user type some input I do following.

[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

When I execute above code debugger gives me breaking constraints warnings.

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.

Please note that these warnings are only appearing if I call layoutIfNeeded() method.

In my openion there are no conflicting constraints. But if someone can help me that will be highly appriciated.

like image 684
Madu Avatar asked Feb 01 '26 06:02

Madu


1 Answers

You have to set active flag to false before you turn on some constraints. Example:

self.inputViewConstraint.active = false
self.inputViewWithErrorConstraint.active = false
self.inputViewConstraint.active = result == NBValidationResultOK
self.inputViewWithErrorConstraint.active = !self.inputViewConstraint.active

In case where you turn on both constrainst on same time you will get warnings.

like image 195
Daniel Sumara Avatar answered Feb 02 '26 20:02

Daniel Sumara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!