ios 无法同时满足约束 - 没有适当的约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14327145/
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 - No constraints in place
提问by The Crazy Chimp
I have gone through and removed every single user constraint yet I am still getting the following error ONLYafter I rotate the device. I have absolutely no clue why though. Does anyone have any ideas?
我已经完成并删除了每个用户约束,但仅在旋转设备后我仍然收到以下错误。我完全不知道为什么。有没有人有任何想法?
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.
回答by jrturton
Let's look at these one by one.
让我们一一看看这些。
"<NSAutoresizingMaskLayoutConstraint:0x84543d0 h=--& v=--& V:[UIView:0xa330270(768)]>"
"<NSAutoresizingMaskLayoutConstraint:0x84543d0 h=--& v=--& V:[UIView:0xa330270(768)]>"
This is saying view 0xa330270 (A) must be 768 points high.
这就是说视图 0xa330270 ( A) 必须是 768 点高。
"<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-| (Names: '|':UIView:0xa330270 )>"
"<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-| (Names: '|':UIView:0xa330270 )>"
This is saying view 0xa331260 (B)'s bottom edge must be a gap of -1 from the bottom of A, which is it's superview.
这就是说视图 0xa331260 ( B) 的底部边缘必须与A的底部有 -1 的间隙,这是它的超级视图。
"<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260] (Names: '|':UIView:0xa330270 )>"
"<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260] (Names: '|':UIView:0xa330270 )>"
This is saying that B's top edge must be a gap of 841 points from the top of its superview, A.
这就是说B的顶部边缘与其父视图A的顶部必须有 841 点的间隙。
These three things can't all be true - Acan't be 768 points high, and contain a subview with a top edge 841 points inset from the top and -1 points inset from the bottom. Where have you defined each of these constraints?
这三件事不可能都是真的 - A不能高 768 点,并且包含一个子视图,其顶部边缘距顶部插入 841 点,底部插入 -1 点。你在哪里定义了这些约束?
You haven't said what layout you are trying to achieve, but it looks like you might have an autoresizing mask on the superview that is preventing it changing in height when you rotate the device. As far as I know the autoresizing constraints only appear if you have added views programmatically, since a storyboard or xib is either all-autolayout, or not. Unless you are doing something like adding an auto laid out view (loaded from a nib?) to another view from a non-autolayout nib?
您还没有说明您要实现的布局,但看起来您在超级视图上可能有一个自动调整大小的蒙版,可以防止它在旋转设备时改变高度。据我所知,仅当您以编程方式添加视图时才会出现自动调整大小约束,因为故事板或 xib 要么是全自动布局,要么不是。除非您正在执行诸如将自动布局视图(从笔尖加载?)添加到非自动布局笔尖的另一个视图之类的事情?
回答by Hashem Aboonajmi
thanks to http://useYourLoaf.comfor this complete solution:
感谢http://useYourLoaf.com提供了这个完整的解决方案:
http://useyourloaf.com/blog/using-identifiers-to-debug-autolayout.html
http://useyourloaf.com/blog/using-identifiers-to-debug-autolayout.html
A quick tip I found buried in a WWDC 2015 session on Auto Layout that helps when debugging problems with constraints
我在 WWDC 2015 自动布局会议中发现的一个快速提示,它有助于调试约束问题
If you have used Auto Layout you will be familiar with the log that Xcode spits out when you get something wrong. To create an example I modified my Stack View sample codeand added a constraint to each of the images to give them a fixed width of 240 (not a good idea as we will see).
如果您使用过 Auto Layout,您就会熟悉 Xcode 在出现问题时吐出的日志。为了创建一个示例,我修改了我的 Stack View 示例代码,并为每个图像添加了一个约束,使它们具有 240 的固定宽度(正如我们将看到的,这不是一个好主意)。
That works in regular width views such as the iPad but is too wide for a compact width view such as the iPhone in portrait. The console log at runtime is not fun to read. Skipping the boilerplate text you get a list of the problematic constraints:
这适用于常规宽度的视图,例如 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)]>"
The log then tells you which of the above constraints it has decided to break:
然后日志会告诉您它决定打破上述哪些约束:
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fc1ab536ef0 H:[UIImageView:0x7fc1ab537380(240)]>
The log output uses the auto layout visual format language but it is hard to pick out my constraints from those created by the system. This is especially the case with stack views which are by design intended to create most of the constraints for you. In this trivial example I know the fixed width constraints that I just added broke things but it is hard to see that from the log and the more complex the view the harder it gets.
日志输出使用自动布局视觉格式语言,但很难从系统创建的约束中挑选出我的约束。堆栈视图的情况尤其如此,这些视图旨在为您创建大部分约束。在这个简单的例子中,我知道我刚刚添加的固定宽度约束破坏了一些东西,但很难从日志中看出这一点,而且视图越复杂就越难。
Adding an Identifier to a Constraint
向约束添加标识符
The log gets a lot easier to understand if you add an identifier to each constraint (NSLayoutConstraint
has had an identifier property since iOS 7). In Interface Builder find the constraint and add the identifier in the Attributes inspector (I am using $ as a prefix/suffix to make them stand out in the log):
如果您为每个约束添加一个标识符(NSLayoutConstraint
从 iOS 7 开始就有一个标识符属性),日志会更容易理解。在 Interface Builder 中找到约束并在属性检查器中添加标识符(我使用 $ 作为前缀/后缀以使它们在日志中脱颖而出):
Update 18-August-2015: As pointed out in the comments the identifier can only be edited in Interface Builder starting with Xcode 7. It is not visible in Xcode 6.4.
2015 年 8 月 18 日更新:正如评论中所指出的,标识符只能在从 Xcode 7 开始的 Interface Builder 中进行编辑。它在 Xcode 6.4 中不可见。
If adding the constraint in code:
如果在代码中添加约束:
constraint.identifier = "$HeartImageFixedWidth$"
It is trickier if you are using the visual format language which uses arrays of constraints. For example, consider the Swift code fragment to create a fixed width constraint for the heart image view:
如果您使用的是使用约束数组的视觉格式语言,那就更棘手了。例如,考虑为心脏图像视图创建固定宽度约束的 Swift 代码片段:
let heartWidth = NSLayoutConstraint.constraintsWithVisualFormat("[heart(240)]",
options:[], metrics:nil, views:viewsDictionary)
Since heartWidth is an array of type [NSLayoutConstraint] setting the identifier is a little more work:
由于 heartWidth 是 [NSLayoutConstraint] 类型的数组,因此设置标识符需要更多工作:
for constraint in heartWidth {
constraint.identifier = "$HeartImageFixedWidth$"
}
heartImage.addConstraints(heartWidth)
With identifies set for my constraints it is now much easier to find them in the log file (see the first four lines):
为我的约束设置标识后,现在可以更轻松地在日志文件中找到它们(请参阅前四行):
"<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)]>"
It also much clearer which of the constraints the system has chosen to break:
它也更清楚系统选择打破哪些约束:
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7f92a3009be0 '$StarImageFixedWidth$' H:[UIImageView:0x7f92a304d190(240)]>
Adding identifiers to constraints is not without effort but it can pay off the next time you have to sort through the debug log of a complex layout.
将标识符添加到约束并非易事,但下次您必须对复杂布局的调试日志进行排序时,它可以带来回报。
Further Reading
进一步阅读
回答by Naishta
Its worth knowing the basics, and understand what Apple/Xcode is trying to tell you through the logs
了解基础知识并了解 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]
Once you understand this, reading your specific error is pretty easy
一旦你理解了这一点,阅读你的具体错误就很容易了
回答by DrBug
I guess this is not a common error, but I solved it somewhat in a layman way. I was getting cryptic messages like the one above. To make sense of it, I created dummy view classes and attached it to the views in my storyboard. For example, if I had a UIView, I created a class called AddressView and attached it to this view in story board. Its a bit time consuming, but it worked for me. After that instead of object-ids, I got class names which helped me zero in on the views that were causing the issue very easily. My error message now read,
我想这不是一个常见的错误,但我以一种外行的方式解决了它。我收到了像上面那样的神秘消息。为了理解它,我创建了虚拟视图类并将其附加到我的故事板中的视图。例如,如果我有一个 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 )>"
)
Here you can see, the names of my views MainView and Address view are causing the issue.
在这里你可以看到,我的视图 MainView 和 Address 视图的名称导致了这个问题。
To resolve it, I just moved my subview (in this case Address view) and repositioned it back. I think the issue began as I was using a mix of new Automatic Layour in Xcode 4.5 and old skills or manually positioning the views.
为了解决它,我只是移动了我的子视图(在本例中为地址视图)并将其重新定位。我认为问题始于我在 Xcode 4.5 中混合使用新的自动布局和旧技能或手动定位视图。
Anyways, not sure if it was more luck than diligence, but nevertheless this could be a different way of debugging. Maybe this helps someone!
无论如何,不确定是否比勤奋更幸运,但这可能是一种不同的调试方式。也许这对某人有帮助!
回答by tsuz
YourConstraintView.translatesAutoresizingMaskIntoConstraints = NO;
YourConstraintView.translatesAutoresizingMaskIntoConstraints = NO;
Did it for me.
为我做的。
回答by Danil
I've fixed this problem be deleting all translatesAutoresizingMaskIntoConstraints properties from xib file (Open xib as a source code).
我已经解决了这个问题,即从 xib 文件中删除所有 translatesAutoresizingMaskIntoConstraints 属性(将 xib 作为源代码打开)。
回答by nab
One note. You get this error in logs if you are testing using a personal hotspot connection, and the hotspot status bar is at the top. It throws off the constraints.
一注。如果您使用个人热点连接进行测试,并且热点状态栏位于顶部,则会在日志中收到此错误。它摆脱了限制。
Hope this helps someone.. was driving me nuts.
希望这可以帮助某人..让我发疯。
回答by Ali
For me this error was spitted when I gave tableView.estimatedRowHeight = UITableViewAutomaticDimension
对我来说,这个错误是在我给的时候吐出来的 tableView.estimatedRowHeight = UITableViewAutomaticDimension
This should have been tableView.estimatedRowHeight = "Some hardcoded value"
这应该是 tableView.estimatedRowHeight = "Some hardcoded value"
回答by rickrvo
I had this problem and took me 2 days to figure out the source of the problem....
我遇到了这个问题,花了我 2 天的时间才找出问题的根源....
If you open a storyboard programmatically in you code just make sure you do it like this:
如果您在代码中以编程方式打开故事板,请确保您这样做:
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];
I was using the commented line (using presentViewController) and the orientation bug has happening throwing constraints conflicts that weren't my constraints... changing to showViewController all constraints conflicts were gone and orientation works...... (I don't really know why it works with show and not present... still thinking it's... ALIENS...)
我正在使用注释行(使用presentViewController)并且方向错误发生了抛出不是我的约束的约束冲突......更改为showViewController所有约束冲突都消失了并且方向有效......(我不真的知道为什么它适用于表演而不是存在......仍然认为它是......外星人......)
回答by Claytog
This issue of the generated message "Unable to simultaneously satisfy contraints" in the debug console, is also experienced in XCode 9.4.
In my particular instance on the iPad simulator, the message would generate:
1) Only when placing the focus on a particular UITextField.
2) Even with all view contraints removed.
2) Even with all view contraints "Reset to Suggested Contraints".
在调试控制台中生成的消息“无法同时满足约束”的这个问题在 XCode 9.4 中也出现过。
在我在 iPad 模拟器上的特定实例中,消息会生成:
1) 仅当将焦点放在特定的 UITextField 上时。
2)即使删除了所有视图限制。
2)即使所有视图限制“重置为建议的限制”。
However, when the software keyboard was toggled on to display, the message would not be generated. Therefore, how much time should I spend on this issue, that in my instance is only generated when the software keyboard is toggled off.
但是,当打开软键盘进行显示时,不会生成该消息。因此,我应该在这个问题上花多少时间,在我的实例中,只有在关闭软件键盘时才会生成。