ios-无法同时满足约束-p中没有约束

我已经遍历并删除了每个用户约束,但仅在旋转设备后仍然收到以下错误。 我绝对不知道为什么。 有人有什么想法吗?

2013-01-14 21:30:31.363 myApp[35869:c07] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x84543d0 h=--& v=--& V:[UIView:0xa330270(768)]>",
    "<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-|   (Names: '|':UIView:0xa330270 )>",
    "<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260]   (Names: '|':UIView:0xa330270 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-|   (Names: '|':UIView:0xa330270 )>

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.
10个解决方案
96 votes

让我们一一看一下。

"<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260] (Names: '|':UIView:0xa330270 )>"

也就是说,视图0xa330270(A)必须为768点高。

"<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260] (Names: '|':UIView:0xa330270 )>"

也就是说,视图0xa331260(B)的底边与A的底边必须相距-1,这是其超级视图。

"<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260] (Names: '|':UIView:0xa330270 )>"

这就是说,B的顶部边缘必须与其超级视图A的顶部相隔841点。

这三件事不可能全部成立-A不能高768点,并且包含一个子视图,该子视图的顶部边缘从顶部插入841点,从底部插入-1点。 您在哪里定义了这些约束?

您尚未说出要实现的布局,但是看起来您可能在超级视图上具有自动调整大小的蒙版,以防止旋转设备时其高度发生变化。 据我所知,自动调整大小约束仅在您以编程方式添加视图时才会出现,因为情节提要或xib要么是全自动布局,要么不是。 除非您正在执行将非自动布局的笔尖(从笔尖加载?)添加到非自动布局的笔尖的另一个视图中的操作,否则您是否会这么做?

jrturton answered 2019-11-07T19:20:14Z
15 votes

感谢[http://useYourLoaf.com]的完整解决方案:

[http://useyourloaf.com/blog/using-identifiers-to-debug-autolayout.html]

我在WWDC 2015的“自动布局”会话中发现了一个快速技巧,该技巧可在调试约束问题时提供帮助

如果您使用过自动版式,那么您会熟悉错误时Xcode吐出的日志。 为了创建一个示例,我修改了我的Stack View示例代码,并为每个图像添加了约束,以使它们的固定宽度为240(这不是一个好主意,我们将看到)。

enter image description here

该功能可在iPad等常规宽度视图中使用,但对于肖像等iPhone中的紧凑型宽度视图而言则太宽了。 运行时的控制台日志很有趣。 跳过样板文本,您会看到有问题的约束的列表:

"<NSLayoutConstraint:0x7fc1ab520360 H:[UIImageView:0x7fc1ab532650(240)]>",
"<NSLayoutConstraint:0x7fc1ab536ef0 H:[UIImageView:0x7fc1ab537380(240)]>",
"<NSLayoutConstraint:0x7fc1ab545cc0 UIView:0x7fc1ab53d870.trailingMargin == UIStackView:0x7fc1ab53dae0.trailing>",
"<NSLayoutConstraint:0x7fc1ab545d10 UIStackView:0x7fc1ab53dae0.leading == UIView:0x7fc1ab53d870.leadingMargin>",
"<NSLayoutConstraint:0x7fc1ab54e240 'UISV-alignment' UIStackView:0x7fc1ab53dc70.centerX == UIStackView:0x7fc1ab531a10.centerX>",
"<NSLayoutConstraint:0x7fc1ab5167c0 'UISV-canvas-connection' UIStackView:0x7fc1ab531a10.leading == UIImageView:0x7fc1ab532650.leading>",
"<NSLayoutConstraint:0x7fc1ab54ad80 'UISV-canvas-connection' H:[UIImageView:0x7fc1ab537380]-(0)-|   (Names: '|':UIStackView:0x7fc1ab531a10 )>",
"<NSLayoutConstraint:0x7fc1ab5397d0 'UISV-canvas-connection' UIStackView:0x7fc1ab53dae0.leading == _UILayoutSpacer:0x7fc1ab54c3c0'UISV-alignment-spanner'.leading>",
"<NSLayoutConstraint:0x7fc1ab54a4a0 'UISV-canvas-connection' UIStackView:0x7fc1ab53dae0.centerX == UIStackView:0x7fc1ab53dc70.centerX>",
"<NSLayoutConstraint:0x7fc1ab54b110 'UISV-spacing' H:[UIImageView:0x7fc1ab532650]-(16)-[UIImageView:0x7fc1ab537380]>",
"<NSLayoutConstraint:0x7fc1ab548210 'UISV-spanning-boundary' _UILayoutSpacer:0x7fc1ab54c3c0'UISV-alignment-spanner'.leading <= UIStackView:0x7fc1ab531a10.leading>",
"<NSLayoutConstraint:0x7fc1ab551690 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fc1ab53d870(375)]>"

日志然后告诉您已决定打破以上约束中的哪些约束:

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fc1ab536ef0 H:[UIImageView:0x7fc1ab537380(240)]>

日志输出使用自动布局可视格式语言,但是很难从系统创建的约束中找出我的约束。 堆栈视图尤其如此,其目的是为您创建大多数约束。 在这个简单的示例中,我知道我刚刚添加的固定宽度约束会破坏东西,但是很难从日志中看到,并且视图越复杂,难度就越大。

向约束添加标识符

如果您向每个约束添加标识符,则日志变得更容易理解(NSLayoutConstraint自iOS 7起具有标识符属性)。 在Interface Builder中找到约束,然后在“属性”检查器中添加标识符(我使用$作为前缀/后缀以使它们在日志中脱颖而出):

enter image description here

2015年8月18日更新:如注释中所指出,只能在Interface Builder中从Xcode 7开始编辑标识符。在Xcode 6.4中不可见。

如果在代码中添加约束:

constraint.identifier = "$HeartImageFixedWidth$"

如果您正在使用使用约束数组的可视格式语言,则将更加棘手。 例如,考虑使用Swift代码片段为心脏图像视图创建固定宽度约束:

let heartWidth = NSLayoutConstraint.constraintsWithVisualFormat("[heart(240)]", 
                 options:[], metrics:nil, views:viewsDictionary)

由于heartWidth是[NSLayoutConstraint]类型的数组,因此设置标识符要多做一些工作:

for constraint in heartWidth {
  constraint.identifier = "$HeartImageFixedWidth$"
}
heartImage.addConstraints(heartWidth)

通过为我的约束设置了标识,现在可以更轻松地在日志文件中找到它们(请参见前四行):

"<NSLayoutConstraint:0x7f92a305aeb0 '$ContainerStackViewLeading$' UIStackView:0x7f92a3053220.leading == UIView:0x7f92a3052fb0.leadingMargin + 32>",
"<NSLayoutConstraint:0x7f92a305b340 '$ContainerStackViewTrailing$' UIView:0x7f92a3052fb0.trailingMargin == UIStackView:0x7f92a3053220.trailing + 32>",
"<NSLayoutConstraint:0x7f92a301cf20 '$HeartImageFixedWidth$' H:[UIImageView:0x7f92a3047ef0(240)]>",
"<NSLayoutConstraint:0x7f92a3009be0 '$StarImageFixedWidth$' H:[UIImageView:0x7f92a304d190(240)]>",
"<NSLayoutConstraint:0x7f92a3060cc0 'UISV-alignment' UIStackView:0x7f92a30533b0.centerX == UIStackView:0x7f92a30472b0.centerX>",
"<NSLayoutConstraint:0x7f92a301c590 'UISV-canvas-connection' UIStackView:0x7f92a30472b0.leading == UIImageView:0x7f92a3047ef0.leading>",
"<NSLayoutConstraint:0x7f92a305f680 'UISV-canvas-connection' H:[UIImageView:0x7f92a304d190]-(0)-|   (Names: '|':UIStackView:0x7f92a30472b0 )>",
"<NSLayoutConstraint:0x7f92a3064190 'UISV-canvas-connection' UIStackView:0x7f92a3053220.leading == _UILayoutSpacer:0x7f92a30608a0'UISV-alignment-spanner'.leading>",
"<NSLayoutConstraint:0x7f92a30415d0 'UISV-canvas-connection' UIStackView:0x7f92a3053220.centerX == UIStackView:0x7f92a30533b0.centerX>",
"<NSLayoutConstraint:0x7f92a305fa10 'UISV-spacing' H:[UIImageView:0x7f92a3047ef0]-(16)-[UIImageView:0x7f92a304d190]>",
"<NSLayoutConstraint:0x7f92a30508c0 'UISV-spanning-boundary' _UILayoutSpacer:0x7f92a30608a0'UISV-alignment-spanner'.leading <= UIStackView:0x7f92a30472b0.leading>",
"<NSLayoutConstraint:0x7f92a3063240 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7f92a3052fb0(375)]>"

还可以更清楚地了解系统选择打破了哪些约束:

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f92a3009be0 '$StarImageFixedWidth$' H:[UIImageView:0x7f92a304d190(240)]>

将标识符添加到约束并不是一件容易的事,但是下次您必须对复杂布局的调试日志进行排序时,它会有所作为。

进一步阅读

  • WWDC 2015会议219自动布局的奥秘,第2部分
Hashem Aboonajmi answered 2019-11-07T19:22:24Z
9 votes

我猜这不是一个常见的错误,但是我以一种外行的方式解决了。 我收到了类似上面的消息。 为此,我创建了虚拟视图类并将其附加到情节提要中的视图。 例如,如果我有一个UIView,则创建一个名为AddressView的类,并将其附加到故事板上的该视图上。 它有点耗时,但是对我有用。 在那之后,我得到了类名而不是对象ID,这些类名可以帮助我很容易地将引起问题的视图归零。 我的错误讯息现在显示为

2013-07-02 04:16:20.434 Myproject [2908:c07] 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:0x9edeae0 V:|-(0)-[AddressView:0x143ee020]   (Names: '|':MainView:0x129eb6a0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x11e998c0 h=--& v=--& V:[MainView:0x129eb6a0(704)]>",
    "<NSLayoutConstraint:0x156720b0 V:[AddressView:0x143ee020]-(896)-|   (Names: '|':MainView:0x129eb6a0 )>"
)

在这里您可以看到,我的视图名称MainView和Address视图导致了此问题。

为了解决这个问题,我只是移动了子视图(在本例中为“地址”视图)并将其重新定位。 我认为问题始于我在Xcode 4.5中使用新的自动Layour和旧技能的结合,或者手动定位视图。

无论如何,不知道它是否比勤奋更幸运,但是尽管如此,这可能是另一种调试方式。也许这可以帮助别人!

DrBug answered 2019-11-07T19:23:08Z
7 votes

值得了解基础知识,并了解Apple / Xcode试图通过日志告诉您什么

H = Horizontal constraint(for leading and Trailing)
V = Vertical constraint(top and bottom edge)
h = height
w = width

TopEdge    -> V:|-(points)-[VIEW:memoryAddress] 
BottomEdge -> V:[VIEW:memoryAddress]-(points)-|
Leading    -> H:|-(points)-[VIEW:memoryAddress] 
Trailing   -> H:[VIEW:memoryAddress] -(points)-|
height     -> h= --& v=--& V:[VIEW:memoryAddress((points)] 
width      -> VIEW:memoryAddress.width == points 
between    -> H:[VIEW 1]-(51)-[VIEW 2] 

一旦了解了这一点,读取您的特定错误就非常容易

Naishta answered 2019-11-07T19:23:38Z
4 votes

YourConstraintView.translatesAutoresizingMaskIntoConstraints = NO;

为我做了。

tsuz answered 2019-11-07T19:24:09Z
3 votes

我已经解决了这个问题,即从xib文件中删除了所有translationsAutoresizingMaskIntoConstraints属性(将xib作为源代码打开)。

Danil answered 2019-11-07T19:24:33Z
2 votes

一注。 如果使用个人热点连接进行测试,则日志中会出现此错误,并且热点状态栏位于顶部。 它摆脱了约束。

希望这可以帮助某人..让我发疯。

nab answered 2019-11-07T19:25:03Z
0 votes

对我来说,我给tableView.estimatedRowHeight = UITableViewAutomaticDimension时会吐出这个错误

这应该是tableView.estimatedRowHeight = "Some hardcoded value"

Ali answered 2019-11-07T19:25:34Z
0 votes

我遇到了这个问题,花了我两天的时间才找出问题的根源。

如果您在代码中以编程方式打开情节提要,请确保按以下方式进行操作:

UIStoryboard *story = [UIStoryboard storyboardWithName:@"MovieMaker" bundle:nil];
    UIViewController *vc = [story instantiateInitialViewController];
    //this causes layout to break [self presentViewController:vc animated:YES completion:nil];
    [self showViewController:vc sender:nil];

我正在使用注释行(使用presentViewController),方向错误发生了抛出不是我的约束的约束冲突...更改为showViewController,所有约束冲突都消失了,定向工作……(我没有 真的知道为什么它可以和表演一起使用并且不显示...仍然认为是... ALIENS ...)

rickrvo answered 2019-11-07T19:26:11Z
0 votes

XCode 9.4中也遇到了在调试控制台中生成的消息“无法同时满足约束”的问题。
在我在iPad模拟器上的特定实例中,该消息将生成:
1)仅当将焦点放在特定的UITextField上时。
2)即使删除了所有视图约束。
2)即使所有视图约束都“重置为建议约束”。

但是,当打开软键盘以显示时,将不会生成该消息。因此,在这个问题上我应该花费多少时间,在我的实例中,仅当关闭软件键盘时才会生成该时间。

Claytog answered 2019-11-07T19:27:08Z
translate from https://stackoverflow.com:/questions/14327145/unable-to-simultaneously-satisfy-constraints-no-constraints-in-place