无法同时满足约束,将尝试通过违反约束来恢复

Joh*_*Cox 140 debugging objective-c ios nslayoutconstraint

以下是我在调试区域收到的错误消息.它运行正常,没有任何错误,除了我收到此错误.这会阻止苹果接受应用吗?我如何解决它?

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.
Run Code Online (Sandbox Code Playgroud)

Bar*_*zyk 257

我建议调试并找到哪个约束是"你不想要的".假设您有以下问题:

在此输入图像描述

问题始终是如何找到以下约束和视图.

有两种解决方法:

  1. DEBUG VIEW HIERARCHY (不推荐这种方式)

既然您知道在哪里可以找到意外的约束(PBOUserWorkDayHeaderView),那么有一种方法可以很好地完成这项工作.让我们找到UIViewNSLayoutConstraint以红色矩形.因为我们知道他们在内存中id很容易.

  • 使用Debug View Hierarchy停止应用程序:

在此输入图像描述

  • 找到合适的UIView:

在此输入图像描述

  • 接下来是找到我们关心的NSLayoutConstraint:

在此输入图像描述

如您所见,内存指针是相同的.所以我们知道现在发生了什么.此外,您可以NSLayoutConstraint在视图层次结构中找到 由于它是在View中选择的,因此它也在Navigator中选择.

在此输入图像描述

如果需要,您也可以使用地址指针在控制台上打印:

(lldb) po 0x17dce920
<UIView: 0x17dce920; frame = (10 30; 300 24.5); autoresize = RM+BM; layer = <CALayer: 0x17dce9b0>>
Run Code Online (Sandbox Code Playgroud)

您可以对调试器指向您的每个约束执行相同操作:-)现在您决定如何处理此问题.

  1. 印刷它更好 (我真的推荐这种方式,这是Xcode 7)

    • 为视图中的每个约束设置唯一标识符:

在此输入图像描述

  • 创建简单的扩展NSLayoutConstraint:

SWIFT:

extension NSLayoutConstraint {

    override public var description: String {
        let id = identifier ?? ""
        return "id: \(id), constant: \(constant)" //you may print whatever you want here
    }
}
Run Code Online (Sandbox Code Playgroud)

Objective-C的

@interface NSLayoutConstraint (Description)

@end

@implementation NSLayoutConstraint (Description)

-(NSString *)description {
    return [NSString stringWithFormat:@"id: %@, constant: %f", self.identifier, self.constant];
}

@end
Run Code Online (Sandbox Code Playgroud)
  • 再次构建它,现在你有更多可读输出:

在此输入图像描述

  • 一旦你拿到了id它,你可以在Find Navigator中轻松点击它:

在此输入图像描述

  • 并迅速找到它:

在此输入图像描述

怎么简单修复那种情况?

  • 尝试改变优先级,以999断开的约束.

  • 我喜欢第二种方式,我认为它是追踪破坏约束的最佳方式 (2认同)

Dav*_*ong 108

你遇到的问题是NSAutoresizingMaskLayoutConstraints不应该在那里.这是弹簧和支柱的旧系统.要摆脱它,请在您想要约束的每个视图上运行此方法:

[view setTranslatesAutoresizingMaskIntoConstraints:NO];
Run Code Online (Sandbox Code Playgroud)

  • 将setTranslatesAutoresizingMaskIntoConstraints设置为NO时,视图根本没有出现的原因是什么? (5认同)

ped*_*uan 28

请注意,不要在同一方向和类型中使用多个约束.

例如: 尾随的垂直约束= 15,另一个> = 10.

有时,Xcode会创建一些您不会注意到的约束.你必须摆脱冗余约束,日志警告肯定会消失.

在此输入图像描述

另外,您可以直接从日志中读取并检测某些原因:

NSLayoutConstraint:0xa338390 V:| - (15) - [UILabel:0xa331260](名称:'|':UILabel:0xa330270)>

我们可以将其视为UILabel约束中的问题,它导致垂直约束为15pt长.

NSLayoutConstraint:0x859ab20 H :-(13) - | [UIView:0x85a8fb0] ...

这将是尾随水平约束等.


C B*_*B J 6

我抛出了相当多的异常,我发现解决它们的最快和最简单的方法是在例外中找到唯一值,然后我在storyboard源代码中搜索.这有助于我找到导致问题的实际视图和约束(我在所有视图上使用有意义的userLabel,这使得跟踪约束和视图变得更加容易)...

因此,使用上述例外情况,我会将故事板作为xcode(或其他编辑器)中的"源代码"打开,并寻找我能找到的东西......

<NSLayoutConstraint:0x72bf860 V:[UILabel:0x72bf7c0(17)]> 
Run Code Online (Sandbox Code Playgroud)

..这看起来像UILabel上的垂直(V)约束,值为(17).

通过例外我也发现

<NSLayoutConstraint:0x72c22b0 V:[UILabel:0x72bf7c0]-(NSSpace(8))-[UIButton:0x886efe0]>
Run Code Online (Sandbox Code Playgroud)

看起来像UILabel(0x72bf7c0)接近UIButton(0x886efe0),有一些垂直间距(8).

希望这足以让我在故事板源代码中找到特定的视图(可能通过最初搜索文本"17"),或者至少是一些可能的候选者.从那里我应该能够真正弄清楚它们在故事板中的哪些视图,这将使识别问题变得容易得多(寻找"重复"固定或固定与大小限制冲突).


Ron*_*oh1 6

我很难搞清楚导致此错误的约束条件.这是一种更简单的方法.

我正在使用Xcode 6.1.1

  1. "Command + A"选择所有UILabels,UIImages等.
  2. 单击编辑器 - > Pin>(选择...)到Superview
  3. 再次单击编辑器 - >解决自动布局问题 - >添加缺失约束或重置为建议的约束.这取决于你的情况.


Sté*_*ert 5

我有这个问题,因为我的.xib文件使用autolayout.

在文件检查器中,第一个选项卡.没有用"使用Autolayout"解决了这个问题.

自动布局文件检查器


roy*_*emi 5

这是我的经验和解决方案。我没有碰代码

  1. 选择视图(UILabel、UIImage 等)
  2. 编辑器 > 固定 >(选择...)到超级视图
  3. 编辑器 > 解决自动布局问题 > 添加缺少的约束


Aru*_*r P 5

快速使用此代码

view.translatesAutoresizingMaskIntoConstraints = false
Run Code Online (Sandbox Code Playgroud)

  • @SmokeDispenser 无论如何,我已经提到这是一个快速代码。它对我有用。swift/objective 它具有通用的可可触摸 SDK。 (3认同)