使用自动布局出错,但在模拟器中看起来不错

Getting error using auto-layout, but looks good in simulator

我正在尝试在 iPhone 的方向发生变化时更改 UIView 的位置。当 iPhone 处于纵向模式时,我希望 container view 位于 tableView 之上。在横向模式下,它们应该并排(Container view:左;tableView:右)。我想使用 auto-layout.

来做到这一点

这是我如何设置它的图片:(图片取自:w Any h Compact。)

在模拟器中,一切都按预期工作。但是当 iPhones 方向改变时,我得到以下错误:

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) 
(
    "<NSLayoutConstraint:0x7f83f254b7d0 V:[UIView:0x7f83f2564230(272)]>",
    "<NSLayoutConstraint:0x7f83f251d9f0 V:[_UILayoutGuide:0x7f83f250c270]-(0)-[UIView:0x7f83f2564230]>",
    "<NSLayoutConstraint:0x7f83f244bd00 UITableView:0x7f83f28c8e00.bottom == _UILayoutGuide:0x7f83f252f700.top>",
    "<NSLayoutConstraint:0x7f83f244bd50 V:[UIView:0x7f83f2564230]-(0)-[UITableView:0x7f83f28c8e00]>",
    "<_UILayoutSupportConstraint:0x7f83f254b880 V:[_UILayoutGuide:0x7f83f250c270(64)]>",
    "<_UILayoutSupportConstraint:0x7f83f24e4aa0 V:|-(0)-[_UILayoutGuide:0x7f83f250c270]   (Names: '|':UIView:0x7f83f2567df0 )>",
    "<_UILayoutSupportConstraint:0x7f83f251d8e0 V:[_UILayoutGuide:0x7f83f252f700(49)]>",
    "<_UILayoutSupportConstraint:0x7f83f2564300 _UILayoutGuide:0x7f83f252f700.bottom == UIView:0x7f83f2567df0.bottom>",
    "<NSLayoutConstraint:0x7f83f257dec0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7f83f2567df0(375)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f83f254b7d0 V:[UIView:0x7f83f2564230(272)]>

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.

错误可能很简单,但我不明白。 (同样,在模拟器中一切看起来都很好很正常。)

这个错误只是意味着你有太多的约束,其中一些发生了冲突(无法实现)。系统自动忽略了其中一个冲突约束(特别是表示 UIView 高度为 272 的约束)。即使它对你来说工作得很好,你也应该努力修复它。

问题很可能是你有跨越整个屏幕高度的距离约束,所以一切都从上到下受到约束,但是你也有定义高度的约束,具体的数字使它变得不可能为了满足这两个条件。

Image is taken in: w Any h Compact

但问题是当你切换到另一个方向时,你不是 in w:Any h:Compact。所以在那一刻,一些灰显的约束spring要生了!它们是条件约束,基于大小class,即方向。

所以,您看到屏幕截图顶部的灰色约束,将容器视图高度设置为 272?那就是问题所在!当您旋转方向时,该约束 spring 变为现实,并导致冲突。因此,切换到容器视图高度约束 not 变灰的任何大小 class,并考虑 that 组约束。