2016-08-09 9 views
1

xibファイルに次のビューを作成しています。 enter image description here 2つの制約があり、同時にそれらのうちの1つを必要とするため、1つをアクティブにして他の制約を無効にします。Autolayout layoutIfNeededは強制的な制約です

これらの制約の1つは、ビュー(トップレベルビュー)view.bottom == inputView.bottomです。この制約は、テキスト入力時にユーザーの入力が正しい場合に有効になります。ユーザーが何らかの特殊文字を入力すると、エラーメッセージが表示され、この制約は無効化され、もう1つの制約はview.bottom == errorLabel.bottomになります。もともと、これらの制約は両方ともxibファイルにありますが、view.bottom == errorLabel.bottom750優先度に設定され、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()メソッドを呼び出すと表示されます。

私のopenionには、矛盾する制約はありません。しかし、誰かが私を助けることができれば、それは非常にappriciatedされます。あなたの場合、低優先度で

enter image description here

:制約アクティブ状態は前に変更あなたは低い優先度に制約の1つを変更することができます

:アドバイスとして

+0

必要に応じてレイアウトをアニメーション化しないでください。制約の変更をアニメーション化してみてください! –

+0

@TejaNandamuri animate 'layoutIfNeeded'は正しい方法です。制約の変更はアニメーション化できません。 – kientux

答えて

0

制約はself.inputViewConstraintまたはself.inputViewWithErrorConstraintのいずれかでなければなりません(両方ではありません)。

+0

こんにちは、私はこれを私の心に持っていますが、優先順位を変えることは私の選択肢ではありません。申し訳ありませんが私の質問に言及する必要があります。 – Madu

+0

好奇心が強い、優先事項は何ですか? – LinusGeffarth

+0

'layoutIfNeeded'が呼び出された後のように、' self.inputViewConstraint'と 'self.inputViewWithErrorConstraint'が同時にアクティブになり、コンソールで競合が発生しました。いずれかの制約を低い優先度に変更します。この制約が矛盾している場合は、優先順位の低い制約を無効にして競合を解決してください。 – beeth0ven

0

いくつかの制約をオンにする前に、アクティブフラグをfalseに設定する必要があります。例:

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

constrainstの両方を同時にオンにすると、警告が表示されます。

関連する問題