相同的自动布局约束在纵向和横向中的表现不同

时间:2012-10-22 06:18:48

标签: iphone autolayout

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

Constraint for second view

Constraint for first view

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

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.

我的问题是,当我以纵向模式执行并旋转到横向时,为什么它会抛出异常?为什么约束的行为会有所不同?

1 个答案:

答案 0 :(得分:3)

系统会自动将默认大小调整掩码转换为约束,然后与您的约束冲突。为{2}的子视图添加[view setTranslatesAutoresizingMaskIntoConstraints:NO]调用,NSAutoresizingMaskLayoutConstraints将会消失,并带有异常。