【问题标题】:Getting error using auto-layout, but looks good in simulator使用自动布局出现错误,但在模拟器中看起来不错
【发布时间】:2015-05-08 19:04:29
【问题描述】:

当 iPhone 的方向改变时,我试图改变 UIViews 的位置。当 iPhone 处于纵向模式时,我希望 container view 位于 tableView 之上。在横向模式下,它们应该并排(Container view:左;tableView:右)。我想使用auto-layout 来做到这一点。

这是我如何设置的图片:(图片采用:w Any h Compact。)

在模拟器中,一切都按预期工作。但是当 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.

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

【问题讨论】:

    标签: objective-c xcode nslayoutconstraint autolayout


    【解决方案1】:

    图像采用:w 任意 h 紧凑

    但问题是,当你切换到另一个方向时,你不是在 w:Any h:Compact 中。所以在那一刻,一些灰显的约束突然出现了!它们是条件约束,基于尺寸类,即方向。

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

    【讨论】:

    • 感谢您的回答!!如果在w Any h Compact 中消失,为什么会出现问题?此外,我需要该视图中的容器视图高度为 272。我能做些什么来解决这个问题?
    • 只有当它出现时才会出问题。当它发生时,就像 Will M. 告诉你的那样:你的约束是过度确定的,不能同时满足所有约束。正如他假设的那样,您可能有一个顶部约束和一个底部约束高度约束,这是错误的。
    【解决方案2】:

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

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

    【讨论】:

    • 解释得很漂亮。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多