ios 无法同时满足约束,将尝试通过打破约束来恢复
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11664115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to simultaneously satisfy constraints, will attempt to recover by breaking constraint
提问by Johnny Cox
Below is the error message I receive in the debug area. It runs fine and nothing is wrong except that I receive this error. Would this prevent apple accepting the app? How do I fix it?
下面是我在调试区收到的错误消息。它运行良好,除了我收到此错误外,没有任何问题。这会阻止苹果接受该应用程序吗?我如何解决它?
2012-07-26 01:58:18.621 Rolo[33597:11303] 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:0x887d630 h=--& v=--& V:[UIButtonLabel:0x886ed80(19)]>",
"<NSAutoresizingMaskLayoutConstraint:0x887d5f0 h=--& v=--& UIButtonLabel:0x886ed80.midY == + 37.5>",
"<NSAutoresizingMaskLayoutConstraint:0x887b4b0 h=--& v=--& V:[UIButtonLabel:0x72bb9b0(19)]>",
"<NSAutoresizingMaskLayoutConstraint:0x887b470 h=--& v=--& UIButtonLabel:0x72bb9b0.midY == - 0.5>",
"<NSLayoutConstraint:0x72bf860 V:[UILabel:0x72bf7c0(17)]>",
"<NSLayoutConstraint:0x72c2430 UILabel:0x72bfad0.top == UILabel:0x72bf7c0.top>",
"<NSLayoutConstraint:0x72c2370 UILabel:0x72c0270.top == UILabel:0x72bfad0.top>",
"<NSLayoutConstraint:0x72c22b0 V:[UILabel:0x72bf7c0]-(NSSpace(8))-[UIButton:0x886efe0]>",
"<NSLayoutConstraint:0x72c15b0 V:[UILabel:0x72c0270]-(NSSpace(8))-[UIRoundedRectButton:0x72bbc10]>",
"<NSLayoutConstraint:0x72c1570 UIRoundedRectButton:0x72bbc10.baseline == UIRoundedRectButton:0x7571170.baseline>",
"<NSLayoutConstraint:0x72c21f0 UIRoundedRectButton:0x7571170.top == UIButton:0x886efe0.top>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x72bf860 V:[UILabel:0x72bf7c0(17)]>
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.
回答by Bart?omiej Semańczyk
I would recommend to debug and find which constraint is "the one you don't want". Suppose you have following issue:
我建议调试并找到哪个约束是“你不想要的”。假设您有以下问题:
Always the problem is how to find following Constraints and Views.
问题始终是如何找到以下约束和视图。
There are two solutions how to do this:
有两种解决方案如何做到这一点:
- DEBUG VIEW HIERARCHY(Do not recommend this way)
- DEBUG VIEW HIERARCHY (不推荐这种方式)
Since you know where to find unexpected constraints (PBOUserWorkDayHeaderView) there is a way to do this fairly well. Lets find UIView
and NSLayoutConstraint
in red rectangles. Since we know their id in memoryit is quite easy.
由于您知道在哪里可以找到意外约束 (PBOUserWorkDayHeaderView),因此有一种方法可以很好地做到这一点。让我们在红色矩形中找到UIView
和NSLayoutConstraint
。因为我们知道他们在内存中的id,所以很容易。
- Stop app using Debug View Hierarchy:
- 使用Debug View Hierarchy停止应用程序:
- Find the proper UIView:
- 找到合适的 UIView:
- The next is to find NSLayoutConstraint we care about:
- 接下来就是找到我们关心的NSLayoutConstraint:
As you can see, the memory pointers are the same. So we know what is going on now. Additionally you can find NSLayoutConstraint
in view hierarchy. Since it is selected in View, it selected in Navigator also.
如您所见,内存指针是相同的。所以我们知道现在发生了什么。此外,您可以NSLayoutConstraint
在视图层次结构中找到。由于它是在 View 中选择的,因此它也在 Navigator 中选择了。
If you need you may also print it on console using address pointer:
如果您需要,您也可以使用地址指针在控制台上打印它:
(lldb) po 0x17dce920
<UIView: 0x17dce920; frame = (10 30; 300 24.5); autoresize = RM+BM; layer = <CALayer: 0x17dce9b0>>
You can do the same for every constraint the debugger will point to you:-) Now you decide what to do with this.
您可以对调试器指向您的每个约束执行相同的操作:-) 现在您决定如何处理它。
PRINT IT BETTER(I really recommend this way, this is of Xcode 7)
- set unique identifier for every constraint in your view:
更好地打印(我真的推荐这种方式,这是 Xcode 7)
- 为视图中的每个约束设置唯一标识符:
- create simple extension for
NSLayoutConstraint
:
- 创建简单的扩展
NSLayoutConstraint
:
SWIFT:
斯威夫特:
extension NSLayoutConstraint {
override public var description: String {
let id = identifier ?? ""
return "id: \(id), constant: \(constant)" //you may print whatever you want here
}
}
OBJECTIVE-C
目标-C
@interface NSLayoutConstraint (Description)
@end
@implementation NSLayoutConstraint (Description)
-(NSString *)description {
return [NSString stringWithFormat:@"id: %@, constant: %f", self.identifier, self.constant];
}
@end
- build it once again, and now you have more readable output for you:
- 再次构建它,现在你有更多的可读的输出为您提供:
- once you got your
id
you can simple tap it in your Find Navigator:
- 获得后,您
id
可以在Find Navigator 中简单地点击它:
- and quickly find it:
- 并快速找到它:
HOW TO SIMPLE FIX THAT CASE?
如何简单地解决这个问题?
- try to change priorityto
999
for broken constraint.
- 尝试改变优先级,以
999
断开的约束。
回答by David Wong
The problem you're having is the NSAutoresizingMaskLayoutConstraints should not be in there. This is the old system of springs and struts. To get rid of it, run this method on each view that you're wanting to constrain:
您遇到的问题是 NSAutoresizingMaskLayoutConstraints 不应该在那里。这是弹簧和支柱的旧系统。要摆脱它,请在您要约束的每个视图上运行此方法:
[view setTranslatesAutoresizingMaskIntoConstraints:NO];
回答by pedrouan
Be careful, that you do not use more than one constraint in the same direction and type.
请注意,不要在同一方向和类型上使用多个约束。
For example: Vertical constraint for trailing = 15 and another one is >= 10.
例如: 尾随的垂直约束 = 15,另一个是 >= 10。
Sometimes, Xcode creates some constraints you don't notice. You have to get rid of redundant constraintsand the log warning will surely disappear.
有时,Xcode 会创建一些您没有注意到的约束。你必须摆脱多余的约束,日志警告肯定会消失。
Additionaly, you can read and detectsome certain reasons, directly from the log:
另外,您可以直接从日志中读取和检测某些原因:
NSLayoutConstraint:0xa338390 V:|-(15)-[UILabel:0xa331260] (Names: '|':UILabel:0xa330270 )>
NSLayoutConstraint:0xa338390 V:|-(15)-[UILabel:0xa331260] (Names: '|':UILabel:0xa330270 )>
This we can read as problem in UILabel constraint, it is leading vertical constraint being 15pt long.
这我们可以理解为 UILabel 约束中的问题,它是前导垂直约束,长度为 15pt。
NSLayoutConstraint:0x859ab20 H:-(13)-|[UIView:0x85a8fb0]...
NSLayoutConstraint:0x859ab20 H:-(13)-|[UIView:0x85a8fb0]...
This would be trailing horizontal constraint etc.
这将是尾随水平约束等。
回答by C James
I had quite a number of these exceptions thrown, the fastest and easiest way I found to solve them was to find unique values in the exceptions which I then searched for in the storyboard source code. This helped me to find the actual view(s) and constraint(s) causing the problem (I use meaningful userLabels on all of the views, which makes it a lot easier to track the constraints and views)...
我抛出了很多这样的异常,我发现解决它们的最快和最简单的方法是在异常中找到唯一值,然后我在故事板源代码中搜索这些值。这帮助我找到了导致问题的实际视图和约束(我在所有视图上使用有意义的 userLabels,这使得跟踪约束和视图变得更加容易)...
So, using the above exceptions I would open the storyboard as "source code" in xcode (or another editor) and look for something I can find...
因此,使用上述例外情况,我将在 xcode(或其他编辑器)中将故事板作为“源代码”打开并寻找我能找到的东西......
<NSLayoutConstraint:0x72bf860 V:[UILabel:0x72bf7c0(17)]>
.. this looks like a vertical (V) constraint on a UILabel with a value of (17).
.. 这看起来像是 UILabel 上的垂直 (V) 约束,值为 (17)。
Looking through the exceptions I also find
查看异常我也发现
<NSLayoutConstraint:0x72c22b0 V:[UILabel:0x72bf7c0]-(NSSpace(8))-[UIButton:0x886efe0]>
Which looks like the UILabel(0x72bf7c0) is close to a UIButton(0x886efe0) with some vertical spacing (8)..
看起来 UILabel(0x72bf7c0) 接近 UIButton(0x886efe0),有一些垂直间距 (8)..
That will hopefully be enough for me to find the specific views in the storyboard source code (probably by searching the text for "17" initially), or at least a few likely candidates. From there I should be able to actually figure out which views these are in the storyboard which will make it a lot easier to identify the problem (look for "duplicated" pinning or pinning that conflicts with size constraints).
希望这足以让我在故事板源代码中找到特定的视图(可能通过最初在文本中搜索“17”),或者至少是一些可能的候选者。从那里我应该能够实际找出故事板中的哪些视图,这将使识别问题变得更加容易(寻找与大小限制冲突的“重复”固定或固定)。
回答by Ronaldoh1
I had a hard time figuring out what constraints were causing this error. Here is a simpler way to do it.
我很难弄清楚是什么限制导致了这个错误。这是一个更简单的方法。
I'm using Xcode 6.1.1
我正在使用 Xcode 6.1.1
- "Command + A" to select all the UILabels, UIImages etc.
- Click Editor -> Pin > (Select...) to Superview
- again click Editor -> Resolve Auto Layout Issues -> Add Missing Constraints or Reset to Suggested Constraints. It depends on your case.
- “Command + A”选择所有 UILabels、UIImages 等。
- 单击 Editor -> Pin > (Select...) to Superview
- 再次单击 Editor -> Resolve Auto Layout Issues -> Add Missing Constraints or Reset to Suggested Constraints。这取决于你的情况。
回答by royemi
Here is my experience and Solution. I didn't touched code
这是我的经验和解决方案。我没有碰代码
- Select view (UILabel, UIImage etc)
- Editor > Pin > (Select...) to Superview
- Editor > Resolve Auto Layout Issues > Add Missing Constraints
- 选择视图(UILabel、UIImage 等)
- 编辑器 > 固定 >(选择...)到超级视图
- 编辑器 > 解决自动布局问题 > 添加缺少的约束
回答by Stéphane Bruckert
I had this issue because my .xib
files were using autolayout.
我遇到这个问题是因为我的.xib
文件使用了自动布局。
In the file inspector, first tab. Unticking "Use Autolayout" solved the problem.
在文件检查器中,第一个选项卡。取消勾选“使用自动布局”解决了这个问题。
回答by Arun Kumar P
use swift this code
快速使用此代码
view.translatesAutoresizingMaskIntoConstraints = false
回答by Kampai
I have followed SO questions and answers from each search query. But they all are related with specific one.
我遵循了每个搜索查询中的 SO 问题和答案。但它们都与特定的相关。
At the basic, I mean before you are going to write down a format (may be a simple one) it will gives you a warnings.
基本上,我的意思是在你准备写下一种格式(可能是一个简单的格式)之前,它会给你一个警告。
From iOS 8.0 by default views are size classes. Even if you disable size classes it will still contains some auto layout constraints.
从 iOS 8.0 开始,默认视图是大小类。即使您禁用尺寸类,它仍然会包含一些自动布局约束。
So if you are planning to set constrains via code using VFL. Then you must take care of one below line.
因此,如果您打算使用 VFL 通过代码设置约束。那么你必须照顾一个线下。
// Remove constraints if any.
[self.view removeConstraints:self.view.constraints];
I had search a lot in SO, but the solution was lies in Apple Sample Code.
我在 SO 中进行了大量搜索,但解决方案在于Apple Sample Code。
So you must have to remove default constraints before planning to add new one.
因此,您必须在计划添加新约束之前删除默认约束。
回答by giuseppe
For me the main reason of this problem was that I forgot to uncheck AutoLayout
in the Xib
editor.
In fact, I did a lot of adjustments of the XIB
in code.
对我来说,这个问题的主要原因是我忘记AutoLayout
在Xib
编辑器中取消选中。其实我对XIB
in代码做了很多调整。