【问题标题】:Identifying constraints when getting "Unable to simultaneously satisfy constraints"在“无法同时满足约束”时识别约束
【发布时间】:2016-02-09 02:39:11
【问题描述】:

我在将某些视图旋转到横向时收到此日志:

无法同时满足约束。 以下列表中的至少一个约束可能是您不想要的。试试这个:(1)查看每个约束并尝试找出您不期望的; (2) 找到添加了一个或多个不需要的约束的代码并修复它。 (注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)

(
"<NSLayoutConstraint:0x17009b3f0 V:[UILabel:0x14c51ed20'Welcome to...'(>=54)]>",
"<NSLayoutConstraint:0x17009b580 V:[MyApp.LogoHeaderView:0x14c51f3e0]-(82)-[UILabel:0x14c51ed20'Welcome to...']>",
"<NSLayoutConstraint:0x17009b2b0 V:[UILabel:0x14c51ed20'Welcome to...']-(30)-[UIView:0x14c51e7e0]>",
"<NSLayoutConstraint:0x170280230 V:[MyApp.LogoHeaderView:0x14c51f3e0]-(0)-[UIView:0x14c51e7e0]>"

)

将尝试通过打破约束来恢复

<NSLayoutConstraint:0x17009b3f0 V:[UILabel:0x14c51ed20'Welcome to...'(>=54)]>

在 UIViewAlertForUnsatisfiableConstraints 创建一个符号断点,以便在调试器中捕获它。 中列出的 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法也可能会有所帮助。

我什么都不懂。我添加了符号 UIViewAlertForUnsatisfiableConstraints 断点,但内存地址什么也没告诉我……我怎样才能以更不稳定的方式找到该日志所讨论的约束?

提前致谢

【问题讨论】:

标签: ios autolayout breakpoints nslayoutconstraint


【解决方案1】:

您可以对约束重新排序,以便视图以一致的顺序显示。所以,原文:

<NSLayoutConstraint:0x17009b3f0 V:[UILabel:0x14c51ed20'Welcome to...'(>=54)]>
<NSLayoutConstraint:0x17009b580 V:[MyApp.LogoHeaderView:0x14c51f3e0]-(82)-[UILabel:0x14c51ed20'Welcome to...']>
<NSLayoutConstraint:0x17009b2b0 V:[UILabel:0x14c51ed20'Welcome to...']-(30)-[UIView:0x14c51e7e0]>
<NSLayoutConstraint:0x170280230 V:[MyApp.LogoHeaderView:0x14c51f3e0]-(0)-[UIView:0x14c51e7e0]>

变成:

<NSLayoutConstraint:0x17009b580 V:[MyApp.LogoHeaderView:0x14c51f3e0]-(82)-[UILabel:0x14c51ed20'Welcome to...']>
<NSLayoutConstraint:0x17009b3f0 V:[UILabel:0x14c51ed20'Welcome to...'(>=54)]>
<NSLayoutConstraint:0x17009b2b0 V:[UILabel:0x14c51ed20'Welcome to...']-(30)-[UIView:0x14c51e7e0]>

<NSLayoutConstraint:0x170280230 V:[MyApp.LogoHeaderView:0x14c51f3e0]-(0)-[UIView:0x14c51e7e0]>

所以,你有一个垂直的MyApp.LogoHeaderView 实例,它之间有 82 个点和一个标有“欢迎来到...”的标签。该标签的高度大于或等于 54 磅。然后它和UIView 之间有 30 个点。同时,您设置了一个约束条件,即UIView 和您的MyApp.LogoHeaderView 之间不应有距离。冲突在于,一些约束要求MyApp.LogoHeaderViewUIView 之间至少有 82 + 54 + 30 == 166 个点,而另一个约束要求它们之间有 0 个点。

您的 UI 中是否真的有多个位置有 MyApp.LogoHeaderView 和“欢迎来到...”的标签?

【讨论】:

  • 感谢您的详细解释。我在横向时隐藏了标签,所以在隐藏视图时,您是否也应该删除它们的约束?
  • 是的,或者修改约束的constant 属性,如果这样可以实现你想要的。隐藏视图仍然参与自动布局。 (事实上​​,在UILayoutGuide 被引入之前,一些自动布局技术通常涉及隐藏的“间隔”视图。)如果你的目标是iOS 9.0+,你应该考虑UIStackView,它会自动调整视图隐藏.
  • 非常感谢。另一个问题:在设备旋转时更改约束的正确方法是什么?这就是为什么我收到“无法同时满足约束”错误...
【解决方案2】:

当您在 IB 中定义约束时,我认为没有办法获得更多信息。 但问题是什么?你知道标签和视图。现在您可以查看此视图​​的约束条件。 在更糟糕的情况下,删除所有对象并逐个添加它们,并在每次添加下一个项目时运行应用程序。然后你会发现错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2014-10-27
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多