使用自动布局获取错误,但在模拟器中看起来很好

时间:2015-05-08 19:04:30

标签: objective-c xcode autolayout nslayoutconstraint nsautolayout

当iPhone的方向改变时,我正试图改变UIViews的位置。当iPhone处于纵向模式时,我希望container view位于tableView之上。在横向模式下,它们应该并排(Container view:left; tableView:right)。我想使用auto-layout执行此操作。

以下是我如何设置它的图像:(图像被输入:w任何h Compact。)

enter image description here

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

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.

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

2 个答案:

答案 0 :(得分:4)

  

图像被拍摄:w任何h Compact

但问题是当你切换到另一个方向时,你在w:任何h:Compact中。所以在那一刻,一些灰暗的约束栩栩如生!它们是条件约束,基于尺寸等级,即方向。

因此,您会在屏幕截图顶部看到灰色的约束,将容器视图高度设置为272?那就是问题所在!旋转方向时,该约束会变为生命,并导致冲突。因此,切换到容器视图高度约束灰显的大小类,并考虑那个约束集。

答案 1 :(得分:2)

错误只是意味着你有太多的约束,其中一些是碰撞(不可能实现)。系统自动忽略其中一个碰撞约束(特别是表示UIView为高度272的约束)。即使它适合你,你应该努力解决它。

问题很可能是你的距离限制跨越了屏幕的整个高度,所以一切都从上到下受到约束,但是你也有限制高度的约束,而具体的数字使两者都不可能那些要实现的。

相关问题