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

Posted

技术标签:

【中文标题】相同的自动布局约束在纵向和横向中表现不同【英文标题】:Same Auto Layout constraints behaving differently in portrait and landscape 【发布时间】:2012-10-22 06:18:48 【问题描述】:

我正在研究 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.

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

【问题讨论】:

【参考方案1】:

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

【讨论】:

以上是关于相同的自动布局约束在纵向和横向中表现不同的主要内容,如果未能解决你的问题,请参考以下文章

如何使用自动布局为纵向和横向方向配置不同的布局?

iOS 自动布局:如何在 Xcode 故事板中为 iPad 横向和 iPad 纵向设计不同的布局?

具有纵向和横向图像视图的自动布局

自动旋转时自动布局约束更新

如何针对不同的 iPhone 屏幕尺寸在纵向和横向中使用自动布局

纵向而不是横向的自动布局方向更改