【问题标题】:Unable to simultaneously satisfy constraints, will attempt to recover by breaking constraint无法同时满足约束,将尝试通过打破约束来恢复
【发布时间】:2012-07-24 17:36:34
【问题描述】:

以下是我在调试区收到的错误消息。它运行良好,除了我收到此错误之外没有任何问题。这会阻止苹果接受该应用程序吗?我该如何解决?

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.

【问题讨论】:

  • 这个错误只会出现在Xcode 4.5,所以无论如何都不能提交到App Store(直到发布)
  • 那么有什么问题吗?是否可以使用其他版本的 xcode 来提交到应用商店?
  • 是的,有问题,但它无关紧要,因为它与公共 Xcode 版本中尚不可用的 iOS 功能有关。如果它与 4.4 一起编译,您可以使用该版本(今天发布)发布它。
  • 我在 Xcode 4.4 中收到此 OS X 应用程序的错误消息。我不明白。
  • @Bartłomiej Semańczyk 你能不能加入这个房间:chat.stackoverflow.com/rooms/92522/autolayout-and-ios?抱歉,这是随机的,但非常感谢您对自动布局相关的建议,谢谢!

标签: ios objective-c debugging nslayoutconstraint


【解决方案1】:

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

[view setTranslatesAutoresizingMaskIntoConstraints:NO];

【讨论】:

  • 将 setTranslatesAutoresizingMaskIntoConstraints 设置为 NO 时,视图根本没有出现的一些原因是什么?
  • 例如,我创建了一个SSBadgeView(UIView),将一个imageView subView 添加到SSBadgeView,然后将SSBAdgeView 作为子视图添加到一个UIReuseableView。我在 SSBadgeView 和 setTranslatesAutoresizingMaskIntoConstraints:NO 上添加了一些约束。 SSBadgeView 的图像子视图按预期布局,但图像的 SSBadgeView 父级无处可见。
  • 您忘记在子视图中禁用 TranslatesAutoresizingMaskIntoConstraints
  • 这是我设置这个属性时发生的事情,drive.google.com/file/d/0BxuYs8WHXCF7bV9ndHFrQ0dMYVE/… 我正在使用这个代码行:[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO]; for (UIView *view in self.contentView.subviews) { [view setTranslatesAutoresizingMaskIntoConstraints:NO];之前它看起来像:drive.google.com/file/d/0BxuYs8WHXCF7OWlvZGQwN3FlbzQ/… 不过我必须说它确实有效。
【解决方案2】:

我遇到这个问题是因为我的 .xib 文件使用了自动布局。

在文件检查器中,第一个选项卡。取消勾选“使用自动布局”解决了这个问题。

【讨论】:

  • 所以我这样做了,它解决了问题,但是我在屏幕顶部(我用作导航栏)的“UIImageView”被移动到了屏幕底部。你知道为什么会这样吗?
  • Autolayout 使用 constraints 来管理对象的布局。由于您刚刚禁用了自动布局,这些约束很可能已经消失,因此您的 UIImageView 的位置已经改变。您必须再次设置此位置,以编程方式或通过 Interface Builder。
【解决方案3】:

小心,不要在同一方向和类型上使用多个约束。

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

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

此外,您可以直接从日志中读取和检测某些原因

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

这我们可以理解为 UILabel 约束中的问题,它是领先的垂直约束为 15pt 长。

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

这将是尾随水平约束等。

【讨论】:

    【解决方案4】:

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

    1. 选择视图(UILabel、UIImage 等)
    2. Editor > Pin > (Select...) to Superview
    3. 编辑器 > 解决自动布局问题 > 添加缺少的约束

    【讨论】:

      【解决方案5】:

      我抛出了很多这样的异常,我发现解决它们的最快和最简单的方法是在异常中找到唯一值,然后在情节提要源代码中搜索这些值。这帮助我找到了导致问题的实际视图和约束(我在所有视图上都使用了有意义的 userLabels,这使得跟踪约束和视图变得更加容易)...

      因此,使用上述异常,我将在 xcode(或其他编辑器)中将情节提要作为“源代码”打开并寻找我能找到的东西......

      <NSLayoutConstraint:0x72bf860 V:[UILabel:0x72bf7c0(17)]> 
      

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

      查看异常我也发现了

      <NSLayoutConstraint:0x72c22b0 V:[UILabel:0x72bf7c0]-(NSSpace(8))-[UIButton:0x886efe0]>
      

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

      希望这足以让我在情节提要源代码中找到特定视图(可能最初通过在文本中搜索“17”),或者至少找到一些可能的候选者。从那里我应该能够真正弄清楚这些视图在情节提要中哪些视图,这将使识别问题变得更加容易(寻找与尺寸限制冲突的“重复”固定或固定)。

      【讨论】:

      • “我使用有意义的 userLabels”——在这种情况下 userLabels 是什么? (比如你如何设置它们)
      • 在身份检查器的文档部分是一个“标签”字段。它在故事板源代码中被转换为“userLabel”值。
      • 您也可以通过单击左侧“概览”面板中的选定视图来设置它们(有点像在查找器中重命名文件)
      • 在解决可怕的“无法同时满足约束”方面,这可能是我读过的最好的东西。疼痛。我发现此时显示的对象“将尝试通过打破约束
      【解决方案6】:

      对我来说,这个问题的主要原因是我忘记在Xib 编辑器中取消选中AutoLayout。 其实我对代码中的XIB做了很多调整。

      【讨论】:

        【解决方案7】:

        我已经关注了每个搜索查询的 SO 问题和答案。但它们都与特定的相关。

        基本上,我的意思是在你写下一种格式(可能是一种简单的格式)之前,它会给你一个警告。

        从 iOS 8.0 开始,默认视图是尺寸等级。即使您禁用大小类,它仍然会包含一些自动布局约束。

        因此,如果您打算使用 VFL 通过代码设置约束。那么你必须照顾好下面一行。

        // Remove constraints if any.
        [self.view removeConstraints:self.view.constraints];
        

        我在 SO 中进行了很多搜索,但解决方案在于 Apple Sample Code

        因此,在计划添加新约束之前,您必须先删除默认约束。

        【讨论】:

          【解决方案8】:

          我很难弄清楚是什么限制导致了这个错误。这是一种更简单的方法。

          我使用的是 Xcode 6.1.1

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

          【讨论】:

            【解决方案9】:

            我建议调试并找出哪个约束是“你不想要的”。假设您有以下问题:

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

            有两种解决方法:

            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>>
            

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

            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
                }
            }
            

            目标-C

            @interface NSLayoutConstraint (Description)
            
            @end
            
            @implementation NSLayoutConstraint (Description)
            
            -(NSString *)description {
                return [NSString stringWithFormat:@"id: %@, constant: %f", self.identifier, self.constant];
            }
            
            @end
            
            • 再次构建它,现在您可以获得更可读的输出:

            • 获得id 后,您只需在查找导航器中点击它:

            • 并快速找到它:

            如何简单地解决这种情况?

            • 尝试将 priority 更改为 999 以防止约束失效。

            【讨论】:

            • 这只是修复了我的错误...非常感谢。你将如何为目标 c 做到这一点?添加类别?
            • 谢谢解决,但我可以在目标 c 中使用扩展名吗?
            • 该标识符是否与 UIView 上以编程方式可用的 accessibilityIdentifier 相同。如果没有,如何以编程方式设置标识符?
            • @Das 不,不一样。阅读this answer
            • 我喜欢第二种方式,我认为它是追踪破坏约束的最佳方式
            【解决方案10】:
             for(UIView *view in [self.view subviews]) {
                [view setTranslatesAutoresizingMaskIntoConstraints:NO];
            }
            

            这帮助我捕捉到导致问题的视图。

            【讨论】:

              【解决方案11】:

              使用 swift 这个代码

              view.translatesAutoresizingMaskIntoConstraints = false
              

              【讨论】:

              • 这是一个客观 c 标记问题的快速答案。
              • @SmokeDispenser 无论如何我已经提到这是一个快速代码。它对我有用。 swift/objective 有通用的 cocoa touch SDK。
              【解决方案12】:

              需要注意的一件事(至少这让我感到困惑)是我从错误的视图中移除了约束。我试图删除的约束不是我的视图的子约束,所以当我这样做时

              myView.removeConstraint(theConstraint)
              

              它实际上并没有删除任何东西,因为我需要打电话

              myView.superView.removeConstraint(theConstraint)
              

              因为约束在技术上是我的视图的兄弟约束。

              【讨论】:

                【解决方案13】:

                斯威夫特 4

                我只是在 viewDidLoad 中添加了这一行,然后就可以正常工作了。

                view.removeConstraints(view.constraints)
                

                【讨论】:

                  【解决方案14】:

                  上述答案都对我的情况没有帮助。我正在运行 XCode 10.1 并在模拟器上为“iPad(第 5 代)”测试我的应用程序。模拟器运行 iOS 12.1。

                  我的故事板中有一个简单的根视图,有两个 UITextField 子视图。故事板中根本没有使用任何约束。而且我在应用程序或情节提要中没有 UIButtonBarView 对象。

                  当应用启动并放置根视图时,不会打印任何消息。模拟设备旋转时无。

                  但在模拟器中,当我单击其中一个文本字段时,键盘扩展会从屏幕底部出现,尽管不是全键盘,它似乎从未出现在模拟器中。但是在终端上会打印出以下内容:

                  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:0x6000034e7700 h=--& v=--& UIKeyboardAssistantBar:0x7f9c7d714af0.height == 0   (active)>",
                  "<NSLayoutConstraint:0x6000034aba20 V:|-(0)-[_UIUCBKBSelectionBackground:0x7f9c7d51ec70]   (active, names: '|':_UIButtonBarButton:0x7f9c7d51de40 )>",
                  "<NSLayoutConstraint:0x6000034aba70 _UIUCBKBSelectionBackground:0x7f9c7d51ec70.bottom == _UIButtonBarButton:0x7f9c7d51de40.bottom   (active)>",
                  "<NSLayoutConstraint:0x6000034fb3e0 V:|-(0)-[_UIButtonBarStackView:0x7f9c7d715880]   (active, names: '|':UIKeyboardAssistantBar:0x7f9c7d714af0 )>",
                  "<NSLayoutConstraint:0x6000034fb750 V:[_UIButtonBarStackView:0x7f9c7d715880]-(0)-|   (active, names: '|':UIKeyboardAssistantBar:0x7f9c7d714af0 )>",
                  "<NSLayoutConstraint:0x6000034abc00 'UIButtonBar.maximumAlignmentSize' _UIButtonBarButton:0x7f9c7d51de40.height == UILayoutGuide:0x600002ef4e00'UIViewLayoutMarginsGuide'.height   (active)>",
                  "<NSLayoutConstraint:0x6000034d7cf0 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x600002ef4e00'UIViewLayoutMarginsGuide']-(9)-|   (active, names: '|':_UIButtonBarStackView:0x7f9c7d715880 )>",
                  "<NSLayoutConstraint:0x6000034d7c50 'UIView-topMargin-guide-constraint' V:|-(10)-[UILayoutGuide:0x600002ef4e00'UIViewLayoutMarginsGuide']   (active, names: '|':_UIButtonBarStackView:0x7f9c7d715880 )>"
                  )
                  
                  Will attempt to recover by breaking constraint 
                  <NSLayoutConstraint:0x6000034aba70 _UIUCBKBSelectionBackground:0x7f9c7d51ec70.bottom == _UIButtonBarButton:0x7f9c7d51de40.bottom   (active)>
                  
                  Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
                  The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
                  

                  在我看来,所有这一切都与我的应用程序中的任何内容无关,而一切都与 Apple 如何创建自己的键盘视图有关,即使我的小扩展声明要与之结合。

                  所以问题仍然存在,作为应用程序开发人员,我是否有责任做一些事情(假设这是一堆值得关注的事情)还是只是 Apple 自己的问题/错误?

                  FWIW,在模拟较新的 iPad 型号(例如 12.9 英寸 iPad Pro(第 3 代))时,不会出现此约束问题消息。但是在模拟 iPad Pro 9.7 英寸时确实会显示该消息。所有人都声称他们正在运行 iOS 12.1。

                  【讨论】:

                    【解决方案15】:

                    对于 xib 中的 viewCircle,我也遇到了同样的问题,即打破日志中的约束。我几乎尝试了上面列出的所有东西,但没有任何东西对我有用。 然后我尝试更改日志中破坏的高度约束的优先级(通过在 xib 上添加约束的标识符来确认)enter image description here

                    【讨论】:

                      【解决方案16】:

                      我遇到了同样的错误,但仅在特定视图上,当我触摸第一个文本字段,然后触摸下一个文本字段时。

                      我正在为 iOS 13.4 编写 SwiftUI

                       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. 
                          (
                              "<NSLayoutConstraint:0x2809b6760 'assistantHeight' TUISystemInputAssistantView:0x105710da0.height == 44   (active)>",
                              "<NSLayoutConstraint:0x2809ccff0 'assistantView.bottom' TUISystemInputAssistantView:0x105710da0.bottom == _UIKBCompatInputView:0x10525ae10.top   (active)>",
                              "<NSLayoutConstraint:0x2809cccd0 'assistantView.top' V:|-(0)-[TUISystemInputAssistantView:0x105710da0]   (active, names: '|':UIInputSetHostView:0x105215010 )>",
                              "<NSLayoutConstraint:0x2809ca300 'inputView.top' V:|-(0)-[_UIKBCompatInputView:0x10525ae10]   (active, names: '|':UIInputSetHostView:0x105215010 )>"
                          )
                      
                          Will attempt to recover by breaking constraint 
                          <NSLayoutConstraint:0x2809ccff0 'assistantView.bottom' TUISystemInputAssistantView:0x105710da0.bottom == _UIKBCompatInputView:0x10525ae10.top   (active)>
                      
                          Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
                          The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
                      

                      【讨论】:

                        【解决方案17】:

                        基本上,您只需从关联视图中移除该约束即可。例如,如果高度约束发出警告,只需将其从您的视图中删除;它不会影响视图。

                        【讨论】:

                          猜你喜欢
                          • 2015-04-18
                          • 1970-01-01
                          • 1970-01-01
                          • 2012-12-28
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 2016-02-09
                          相关资源
                          最近更新 更多