简体   繁体   English

iOS“无法同时满足约束条件”

[英]iOS “Unable to simultaneously satisfy constraints”

I will use the following notation to explain the invovled views: 我将使用以下表示法来解释已入侵的视图:

  • {V} – the 'superview', ie the main view of the root controller {V} –“超级视图”,即根控制器的主视图
  • {Q} – a rectangle at the center of the screen used as a quiz {Q} –屏幕中央的一个矩形,用作测验
  • {W} – a white bar above {Q} {W} – {Q}上方的白条

I'm getting the following output when running on iPad-Air2 simulator: 在iPad-Air2模拟器上运行时,我得到以下输出:

2016-11-03 08:09:07.700117 MyApp[16645:6976134] [LayoutConstraints] 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:0x60000009d600 h=--& v=--& QuizButtons:0x7fcb8e521830.width == 717   (active)>",
    "<NSLayoutConstraint:0x608000281fe0 H:|-(0)-[ImgWhiteBar:0x7fcb8e525020]   (active, names: '|':ViewTestVC:0x7fcb8e639b30 )>",
    "<NSLayoutConstraint:0x6080002820d0 H:[ImgWhiteBar:0x7fcb8e525020]-(0)-|   (active, names: '|':ViewTestVC:0x7fcb8e639b30 )>",
    "<NSLayoutConstraint:0x608000282210 ImgWhiteBar:0x7fcb8e525020.width == 1.07143*QuizButtons:0x7fcb8e521830.width   (active)>",
    "<NSLayoutConstraint:0x60000009b9e0 'UIView-Encapsulated-Layout-Width' ViewTestVC:0x7fcb8e639b30.width == 768   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6080002820d0 H:[ImgWhiteBar:0x7fcb8e525020]-(0)-|   (active, names: '|':ViewTestVC:0x7fcb8e639b30 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

My interpretation to logged constraints is this: 我对记录的约束的解释是:

  1. {Q}: default <h=--& v=--&> , width =717 {Q}:默认<h=--& v=--&> ,宽度= 717
  2. Horiz: {V.lead} -0- {W} Horiz:{V.lead} -0- {W}
  3. Horiz: {W} -0- {V.trail} Horiz:{W} -0- {V.trail}
  4. {W.width} = 1.07 * {Q.width} {W.width} = 1.07 * {Q.width}
  5. {V.width} = 768 {V.width} = 768

Or maybe a bit simpler 也许更简单

  1. {Q.width} must be 717 {Q.width}必须为717
  2. {W.width} must be 768.21531 {W.width}必须为768.21531
  3. {W} must touch both sides of {V} {W}必须碰到{V}的两面
  4. {V.width} = 768 {V.width} = 768

Questions: 问题:

  • Is my interpretation correct? 我的解释正确吗?
  • What is the problem here? 这里有什么问题? It is due to the inaccuracy of the 768.2 vs 768? 是由于768.2与768的不准确性? If yes, who told Xcode to use 717? 如果是,谁告诉Xcode使用717? I told {Q.width} to be {W.width}/[14:15] 我告诉{Q.width}为{W.width} / [14:15]

Will appreciate any help here! 在这里将不胜感激!

EDIT 编辑

Here are three of the constraints: 这是三个约束:

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

The list of the conflicting constraints includes h=--& v=--& which is the default non-autolayout form. 冲突约束的列表包括h=--& v=--& ,这是默认的非自动布局形式。

I was trying to get the initial size and position using auto-layout, and then tried to turn auto-layout off by setting translatesAutoresizingMaskIntoConstraints=YES. 我试图使用自动布局获取初始大小和位置,然后尝试通过设置translatesAutoresizingMaskIntoConstraints = YES来关闭自动布局。 I got an answer in the Apple developer forum saying that in such case I need to remove all constraints for the view, possibly by removing from the superview and adding back. 我在Apple开发人员论坛上得到了一个答案,说在这种情况下,我需要删除视图的所有约束,可能是通过从超级视图中删除并添加回来。

TIP 小费

While investigating this bug, I have found a way to make the constraints-conflict log much easier to understand. 在研究此错误时,我发现了一种使约束冲突日志更易于理解的方法。 The problem is that the views appear anonymous, specifying only the class but not the name. 问题是视图显示为匿名,仅指定类而不指定名称。

To make your views identifiable , open one of your .m files and add a new class for each view you want to identify, like this: 为了使您的视图可识别 ,请打开您的.m文件之一,并为要标识的每个视图添加一个新类,如下所示:

@interface ImgWhiteBar: UIImageView
@end
@implementation ImgWhiteBar
@end    

@interface Spacer1: UIView
@end
@implementation Spacer1
@end

After that, in InterfaceBuilder, select each view, and then at the "Identity Inspector" on the right modify the generic class (UIVIew, UIImageView etc.) into one of the classes you just created. 之后,在InterfaceBuilder中,选择每个视图,然后在右侧的“ Identity Inspector”中,将通用类(UIVIew,UIImageView等)修改为您刚创建的类之一。

Now run again, and Abracadabra - all views are now identified with their custom classes, allowing you to understand what is going on there. 现在再次运行,然后运行Abracadabra-所有视图现在都通过其自定义类进行标识,从而使您能够了解那里发生的情况。

Have fun debugging constraints! 享受有趣的调试约束!

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

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