【问题标题】:Error: Unable to simultaneously satisfy constraints错误:无法同时满足约束
【发布时间】:2013-07-25 19:26:00
【问题描述】:

我收到此错误:

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:0x1600aec0 V:[UIView:0x102021d0]-(0)-|   (Names:    '|':UIView:0x1600a980 )>",
 "<NSLayoutConstraint:0x1600ae80 V:|-(494)-[UIView:0x102021d0]   (Names: '|':UIView:0x1600a980 )>",
"<NSAutoresizingMaskLayoutConstraint:0x1600e8a0 h=-&- v=-&- UIView:0x1600a980.height == UIWindow:0x9e0ea30.height>",
"<NSAutoresizingMaskLayoutConstraint:0x9e2d130 h=--- v=--- V:[UIWindow:0x9e0ea30(480)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1600aec0 V:[UIView:0x102021d0]-(0)-|   (Names: '|':UIView:0x1600a980    )>

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.

产生此错误的视图有 4 个左/右/上/下约束,我无法删除其中任何一个。

我试过了:

[View setTranslatesAutoresizingMaskIntoConstraints:NO];

没有结果。

你知道如何解决这个问题吗?

谢谢!

【问题讨论】:

    标签: iphone ios autolayout


    【解决方案1】:

    问题出在垂直布局上。

    <NSAutoresizingMaskLayoutConstraint:0x9e2d130 h=--- v=--- V:[UIWindow:0x9e0ea30(480)]>
    

    窗口的自动调整大小掩码。固定边距和大小。你无法改变这一点。

    <NSAutoresizingMaskLayoutConstraint:0x1600e8a0 h=-&- v=-&- UIView:0x1600a980.height == UIWindow:0x9e0ea30.height>
    

    自动调整掩码(固定边距,可调整大小的内容)。可能是您的控制器的视图。高度为480(设置为等于窗口大小)。这里没有什么可修复的。

    <NSLayoutConstraint:0x1600aec0 V:[UIView:0x102021d0]-(0)-|   (Names:    '|':UIView:0x1600a980 )>
    <NSLayoutConstraint:0x1600ae80 V:|-(494)-[UIView:0x102021d0]   (Names: '|':UIView:0x1600a980 )>
    

    您会注意到,两个约束都使用相同的视图 ([UIView:0x102021d0]) 执行某些操作,并且具有相同的第二个视图作为参数 (UIView:0x1600a980)。第二个视图是我们控制器的视图。

    这两个约束定义了与第二个视图边缘的距离。第一个定义底部 (0)。第二个定义顶部(494)。如果超级视图的大小是480,这意味着[UIView:0x102021d0] 的高度将等于-14,这会触发该异常。

    如何解决?那么,将494 约束更改为正确的值。有可能你甚至不想要“顶部”约束,也许你想要一个固定的高度。

    问题是如何产生的?您可能为 iPhone 5 创建了约束,然后尝试在 iPhone 4 上运行该应用程序。如果您将 xib 中的模拟大小更改为 iPhone 4,您应该会立即看到问题。

    【讨论】:

    • 这就是解决方案!我已经根据屏幕大小手动调整了 494 约束!谢谢!
    • @condor304 自动布局的重点是避免手动调整视图 :) 尝试找到一种无需手动调整的方法。可能您不想固定顶部距离。您确定不想改为固定高度吗?
    • 我需要的是无论是在 iphone 4 还是 iphone 5 上,视图都以相同的高度保持在屏幕底部。我无法摆脱顶部距离约束: (应用程序上的所有其他视图都可以正常使用自动布局。但不是这个。
    • @condor304 你必须固定高度并离开底部约束。然后应该可以删除顶部约束。不幸的是,Xcode 4.x 中的界面构建器不适合自动布局。
    【解决方案2】:

    显然,仍有一些视图将translatesAutoresizingMaskIntoConstraints 设置为YES,否则您将不会收到该消息。这可能是给你带来问题的视图的超级视图。确保它也不会将其自动调整大小的掩码转换为约束。

    【讨论】:

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