2013-01-14 118 views
50

我所经历和删除每一个用户约束但我仍然收到以下错误ONLY后我旋转设备。但我绝对不知道为什么。有没有人有任何想法?无法同时满足约束条件 - 没有约束到位

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. 
+0

你如何设置这个观点 - 在代码中,IB还是两者的结合?你可以更新你的问题,包括? – jrturton

+0

可能的重复[获取奇怪的错误谈论约束在Xcode](http://stackoverflow.com/questions/11664115/getting-weird-error-talking-about-constraints-in-xcode) –

+0

关于约束问题的答案已清除是在这里:[无法同时满足约束,将尝试通过打破约束恢复](http://stackoverflow.com/q/11664115/2725435) –

回答

89

让我们一个一个来看看。

"<NSAutoresizingMaskLayoutConstraint:0x84543d0 h=--& v=--& V:[UIView:0xa330270(768)]>"

这话说视图0xa330270()必须768点高。

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

这话说视图0xa331260()的底部边缘必须是-1的间隙从底部,这是它的上海华。

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

这是说,的顶边必须是841点从它的父的顶部的间隙,

这三样东西不能全是真实的 - 一个不能768点高,并包含一个子视图与顶部边缘841点,从顶部嵌入且-1分从底部内侧。你在哪里定义了这些约束条件?

你还没说你想达到什么样的布局,但它看起来像你可能会对这是防止它在高度变化,当你旋转设备的上海华盈的自动尺寸调整掩码。据我所知,只有在以编程方式添加视图时才会出现自动调整约束,因为故事板或xib可以是全自动布局,也可以不是。除非你像添加一个自动布局视图(从nib加载?)到另一个非自动布局nib视图?

+2

原来的高度约束设置以及一个约束告诉它伸展到超级观点。为了解决这个问题,我刚刚摆脱了身高限制。谢谢! –

+3

我们如何摆脱高度限制?我面临同样的问题。 –

+2

是否有任何方法来告诉例如实际引用的视图0xa331260? – thumbtackthief

9

我想这不是一个常见的错误,但我是个门外汉方式有所解决它。我收到了像上面那样的神秘消息。为了理解它,我创建了虚拟视图类并将其附加到故事板中的视图中。例如,如果我有一个UIView,我创建了一个名为AddressView的类并将它附加到故事板中的这个视图。它有点耗时,但它对我有用。之后,我不再使用object-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和地址视图的名字引起的问题。

要解决这个问题,我只是提出我的子视图(在这种情况下地址视图)和重新回来。我认为这个问题始于我在Xcode 4.5中使用新的Automatic Layour和旧技能或手动定位视图。

不管怎么说,不知道这是否是比勤奋更运气,但无论如何,这可能是调试的方式不同。也许这有助于某人!

2

我已修正这个问题是删除从XIB文件中的所有属性translatesAutoresizingMaskIntoConstraints(开放XIB作为源代码)。

+0

也可以在代码中设置self.dimmingView.translatesAutoresizingMaskIntoConstraints = NO – malhal

+1

这就是为我解决的!在我的情况下,通过编程将'translatesAutoresizingMaskIntoConstraints'设置为'YES'似乎可行,但我在控制台'无法同时满足约束'(尽管它看起来很好)。我以XML的形式打开,看到很多'translatesAutoresizingMaskIntoConstraints =“NO”'(我怀疑是故事板之间复制的结果,其中只有一个启用了AutoLayout)。从XML中删除之后 - 不再有警告! (我实际上甚至不需要以编程方式做任何事情,'YES'是默认值)。谢谢! –

11

感谢http://useYourLoaf.com这个完整的解决方案:

​​

快速的提示,我发现了埋在自动布局一个WWDC 2015年会议的调试与约束问题的时候,可以帮助

如果您使用自动布局时,您会熟悉Xcode在出现问题时吐出的日志。为了创建一个示例,我修改了my Stack View sample code并为每个图像添加了一个约束,使它们具有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

更新18月 - 2015年:正如在注释中指出的那样,标识符只能在Xcode 7开始的Interface Builder中编辑。它在Xcode 6.4中不可见。

如果添加约束代码:

constraint.identifier = "$HeartImageFixedWidth$" 

这是棘手的,如果你正在使用的使用约束的阵列视觉格式的语言。例如,考虑银行代码片段来创建一个固定的宽度约束对心脏图像视图:

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

由于heartWidth是式[NSLayoutConstraint]的阵列设置标识符是更多一点的工作:

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

随着我的限制,现在更容易找到他们在日志文件中设置标识(见第4行):

"<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)]>" 

还多CLE arer其约束的系统选择了突破:

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

添加标识符来限制也不是没有努力,但它必须通过复杂的布局的调试日志进行排序下一次还清。

进一步阅读

2

一个说明。如果您使用个人热点连接进行测试,并且热点状态栏位于顶部,则会在日志中看到此错误。它抛弃了限制。

希望这可以帮助别人..让我疯狂。

+1

嘿MTB,应该处理这个警告还是预期?我的应用程序设法看起来恰到好处,我不知道应该修复还是忽略它? –

3

YourConstraintView.translatesAutoresizingMaskIntoConstraints = NO;

为我做。

3

它值得了解的基础知识,并了解苹果/ 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] 

一旦你理解了这一点,读你的特定错误是很容易

相关问题