简体   繁体   English

禁用自动版式时,“无法同时满足约束条件”

[英]“Unable to simultaneously satisfy constraints” when AutoLayout is disabled

I have disabled Auto Layout in the interface builder for a specific view: 我已在界面构建器中为特定视图禁用了“自动布局”: 界面构建器

But I get this error in the console, when I load the ViewController for the xib file: 但是,当我为xib文件加载ViewController时,在控制台中出现此错误:

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; 尝试以下操作:(1)查看每个约束,并尝试找出不期望的约束; (2) find the code that added the unwanted constraint or constraints and fix it. (2)查找添加了一个或多个不必要约束的代码并进行修复。 (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) (注意:如果看到的是您不了解的NSAutoresizingMaskLayoutConstraints,请参阅有关UIView属性translationsAutoresizingMaskIntoConstraints的文档)

I am working with frames in this view and I usually don't get any Auto Layout errors when I have disabled it in the Interface Builder. 我正在此视图中使用框架,但是在Interface Builder中禁用它时,通常不会出现任何“自动布局”错误。 This is the constraints that seems to break: 这似乎打破了约束:

(
    "<NSLayoutConstraint:0x170f5cd0 V:[UIView:0x1709e560]-(0)-[UITableView:0x162e2200]>",
    "<NSAutoresizingMaskLayoutConstraint:0x17185130 h=--& v=--& UIView:0x1709e560.midY == + 48>",
    "<NSAutoresizingMaskLayoutConstraint:0x157e29a0 h=--& v=--& V:[UIView:0x1709e560(96)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x171c2670 h=--& v=--& TableHeaderView:0x1705d1c0.midY == + 72.5>",
    "<NSAutoresizingMaskLayoutConstraint:0x171cb510 h=--& v=--& V:[TableHeaderView:0x1705d1c0(145)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x1573fca0 h=-&- v=-&- UITableView:0x162e2200.midY == UIView:0x17505080.midY + 72.5>",
    "<NSAutoresizingMaskLayoutConstraint:0x1573fcd0 h=-&- v=-&- UITableView:0x162e2200.height == UIView:0x17505080.height - 145>",
    "<NSAutoresizingMaskLayoutConstraint:0x171e4450 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x17505080]   (Names: '|':UIViewControllerWrapperView:0x17034d00 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x16c9ad30 V:[UIView:0x16cc2a00]-(0)-[UITableView:0x15249400]>

Correct me if I am wrong, but I only thought I would be getting this error if Auto Layout was enabled? 如果我错了,请更正我,但我只认为如果启用了“自动版式”会出现此错误?

To me it seems like it thinks I have setup a constraint between the UIView and the UITableView , but I havn't! 在我看来,似乎我已经在UIViewUITableView之间设置了约束,但是我没有! Auto Layout is disabled in the Interface Builder and I do not set any constraints in the code. 在界面生成器中禁用了自动布局,并且我没有在代码中设置任何约束。

I have tried setting translatesAutoresizingMaskIntoConstraints to NO , but my frames are not respected when I do this. 我尝试将translatesAutoresizingMaskIntoConstraints设置为NO ,但是这样做时不尊重我的框架。

It seems to me, like the view is working with Auto Layout. 在我看来,就像视图正在使用“自动布局”一样。 I have tried to delete the file and construct it again, but I still get the same error. 我试图删除该文件并再次构造它,但是仍然出现相同的错误。

Why am I getting this error when Auto Layout is disabled in the xib file? 在xib文件中禁用自动布局时,为什么会出现此错误?

EDIT: 编辑:

It turns out that I had made a mistake when I was searching for constraints. 事实证明,我在寻找约束时犯了一个错误。 I found this line in my code: 我在代码中找到了这一行:

NSDictionary* views = @{@"searchHeaderView": self.searchHeader.view, @"tableView": self.tableView};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[searchHeaderView]-(==0)-[tableView]" options:0 metrics:nil views:views]];

I think that explains it :-P 我认为这可以解释它:-P

Something weird is going on, because if you look closely, the memory addresses of at the 'will break' constraint and views attached to it are not in the array, so these are different instances. 发生一种奇怪的情况,因为如果仔细观察,“将中断”约束处的内存地址和附加在其上的视图不在数组中,因此它们是不同的实例。

Regardless, the unwelcome Auto Layout behavior is consistent with your suspicion: If one constraint gets added to the view hierarchy, AutoLayout "turns back on" at least for for that part of it and you get a mess of autoresizingmask constraints. 无论如何,不​​受欢迎的“自动布局”行为都与您的怀疑一致:如果将一个约束添加到视图层次结构,则“自动布局”至少在该部分会“重新打开”,并且会出现一堆自动调整掩码约束的情况。

So this is a debugging issue. 因此,这是一个调试问题。 Some commands methods you can use in the debugger: 您可以在调试器中使用的一些命令方法:

po [[UIWindow keyWindow] recursiveDescription]

Gives you a complete outline of the view hierarchy, with UIView descriptions, which include frames. 通过UIView描述(包括框架)为您提供视图层次结构的完整轮廓。

po [[UIWindow keyWindow] _autolayoutTrace]

This is private API but that's not an issue during development and especially not debugging. 这是私有API,但在开发过程中(尤其是在调试过程中)不是问题。 This actually works best if you follow the suggestion and add a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints (the breakpoints navigator, at the bottom). 如果您遵循建议并在UIViewAlertForUnsatisfiableConstraints(底部的断点导航器)上添加了符号断点,则此方法实际上最有效。 Best of luck. 祝你好运。

I am pretty sure that if you do not want to use AutoLayout then you need to set translatesAutoresizingMaskIntoConstraints to YES rather than NO . 我非常确定,如果您不想使用AutoLayout,则需要将translatesAutoresizingMaskIntoConstraints设置为YES而不是NO

This then ensures that constraints are created for you which reflect the position drawn in interface builder without you having to add constraints. 然后,这确保为您创建了约束,这些约束反映了在界面构建器中绘制的位置,而无需添加约束。 Perhaps you have some constraints assigned which clash with those it thinks it needs to autogenerate? 也许您分配了一些约束,这些约束与它认为需要自动生成的约束冲突? I would remove any constraints, if you can find any. 如果可以找到任何限制,我将删除所有限制。 If there are none to delete, can you post the list it says are clashing? 如果没有要删除的内容,您可以张贴显示冲突的列表吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM