2

我正在研究 iOS 6.0 的新功能 Auto Layout,我创建了一个示例应用程序,它有一个视图控制器和两个 UIView 作为子视图。我提供了以下约束:

第二视图的约束

第一视图的约束

当我在横向模式下执行以下应用程序并将其旋转到纵向时,它可以正常工作,但是当我在纵向模式下执行它并旋转到横向时,它会引发异常:

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

系统会自动将默认的调整大小蒙版转换为约束,然后与您的冲突。为您的两个子视图添加对 NSAutoresizingMaskLayoutConstraints 的调用[view setTranslatesAutoresizingMaskIntoConstraints:NO]将与异常一起消失。

于 2012-10-31T17:14:21.017 回答