2

iOS 6.0 の新機能である自動レイアウトを掘り下げていたので、1 つのビュー コントローラーと 2 つの UIView をサブビューとして持つサンプル アプリを作成しました。次の制約を指定しました。

2 番目のビューの制約

最初のビューの制約

次のアプリを横向きモードで実行して縦向きに回転すると正常に動作しますが、縦向きモードで実行して横向きに回転すると例外がスローされます。

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:0x748cae0 h=--- v=--- H:[UIWindow:0x716a060(768)]>",
    "<NSLayoutConstraint:0x716d980 V:[UIView:0x716d540]-(736)-|   (Names: '|':UIView:0x716d0c0 )>",
    "<NSLayoutConstraint:0x716d8c0 V:|-(20)-[UIView:0x716d540]   (Names: '|':UIView:0x716d0c0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7176020 h=-&- v=-&- UIView:0x716d0c0.width == UIWindow:0x716a060.width - 20>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x716d980 V:[UIView:0x716d540]-(736)-|   (Names: '|':UIView:0x716d0c0 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2012-10-22 11:37:37.297 AutoLayoutSample[679:c07] 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:0x748cae0 h=--- v=--- H:[UIWindow:0x716a060(768)]>",
    "<NSLayoutConstraint:0x716d880 V:[UIView:0x716d230]-(730)-|   (Names: '|':UIView:0x716d0c0 )>",
    "<NSLayoutConstraint:0x716d840 V:|-(20)-[UIView:0x716d230]   (Names: '|':UIView:0x716d0c0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7176020 h=-&- v=-&- UIView:0x716d0c0.width == UIWindow:0x716a060.width - 20>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x716d880 V:[UIView:0x716d230]-(730)-|   (Names: '|':UIView:0x716d0c0 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

私の質問は、縦向きモードで実行して横向きに回転すると例外がスローされるのはなぜですか?また、制約の動作が異なるのはなぜですか?

4

1 に答える 1

3

システムは自動的にデフォルトのサイズ変更マスクを制約に変換しますが、これはユーザーのマスクと競合します。両方のサブビューの呼び出しを追加する[view setTranslatesAutoresizingMaskIntoConstraints:NO]と、例外とともに NSAutoresizingMaskLayoutConstraints がなくなります。

于 2012-10-31T17:14:21.017 に答える