在导航栏中使用提示时自动布局警告

Autolayout warning when using prompt in navigation bar

我正在使用带有提示的导航栏,如下所示:

我在控制台收到以下自动布局警告:

2019-09-01 21:26:03.225576+0200 Hortus[85622:2386450] [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. 
(
    "<NSLayoutConstraint:0x600002d16990 UILabel:0x7fc9eb816f50'Testen'.centerX == _UINavigationBarModernPromptView:0x7fc9ebe27780.centerX   (active)>",
    "<NSLayoutConstraint:0x600002d15c20 UILabel:0x7fc9eb816f50'Testen'.leading >= UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'.leading   (active)>",
    "<NSLayoutConstraint:0x600002d10ff0 '_UITemporaryLayoutWidth' _UINavigationBarModernPromptView:0x7fc9ebe27780.width == 0   (active)>",
    "<NSLayoutConstraint:0x600002d16a30 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UINavigationBarModernPromptView:0x7fc9ebe27780 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600002d15c20 UILabel:0x7fc9eb816f50'Testen'.leading >= UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'.leading   (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.

2019-09-01 21:26:03.226198+0200 Hortus[85622:2386450] [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. 
(
    "<NSLayoutConstraint:0x600002d16990 UILabel:0x7fc9eb816f50'Testen'.centerX == _UINavigationBarModernPromptView:0x7fc9ebe27780.centerX   (active)>",
    "<NSLayoutConstraint:0x600002d15540 UILabel:0x7fc9eb816f50'Testen'.trailing <= UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'.trailing   (active)>",
    "<NSLayoutConstraint:0x600002d10ff0 '_UITemporaryLayoutWidth' _UINavigationBarModernPromptView:0x7fc9ebe27780.width == 0   (active)>",
    "<NSLayoutConstraint:0x600002d14320 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide']-(8)-|(LTR)   (active, names: '|':_UINavigationBarModernPromptView:0x7fc9ebe27780 )>"

    "<NSAutoresizingMaskLayoutConstraint:0x60000043be30 h=--& v=--& _UINavigationBarModernPromptView:0x7fb3c6416640.width == 0   (active)>",
    "<NSLayoutConstraint:0x600000421400 UILabel:0x7fb3c66d8bf0'Plant guide'.centerX == _UINavigationBarModernPromptView:0x7fb3c6416640.centerX   (active)>",
    "<NSLayoutConstraint:0x600000421f90 UILabel:0x7fb3c66d8bf0'Plant guide'.trailing <= UILayoutGuide:0x600001e58fc0'UIViewLayoutMarginsGuide'.trailing   (active)>",
    "<NSLayoutConstraint:0x600000421e00 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001e58fc0'UIViewLayoutMarginsGuide']-(20)-|(LTR)   (active, names: '|':_UINavigationBarModernPromptView:0x7fb3c6416640 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600002d15540 UILabel:0x7fc9eb816f50'Testen'.trailing <= UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'.trailing   (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.

这些警告是针对导航栏中的提示给出的,但我没有为导航栏设置autolayout约束。这应该由 IOS 自己处理。

我该如何解决这些警告?

[编辑] 当转到视图控制器时,警告在 prepare() 中给出。我在 prepare() 中设置提示。 segue 是故事板参考,定义为 'present modally'。故事板引用引用了 UINavigationController。提示设置在导航控制器的根视图控制器上。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let identifier = segue.identifier {
        switch identifier {

        case "editImage":
            let nc = segue.destination as? TTImageNavigationController
            if let rvc = nc?.rootViewController as? TTImageEditorController {
                rvc.navigationItem.prompt = navigationItem.prompt
            }

        default:
            break
        }
    }
}

[编辑] 似乎大多数反应的人都不清楚这个问题。再次......我没有在代码或情节提要中设置任何自动布局设置。在 segue 之前的 prepare() 函数中设置提示后,问题出现在导航栏中。上面给出了代码。任何说我必须更改或检查我的约束的反应都没有帮助,因为没有要检查的约束!

[编辑] 根据 Mohsen 的要求,我在 github 上创建了一个小示例项目:https://bitbucket.org/Leontien/navigationbarautolayoutwarning/src/master/

这不是你的问题。这是苹果的错误。忽略控制台。向 Apple 提交报告并继续前进。

设置约束的优先级。这将根据优先级满足约束条件。请参考 this link.

您为标签设置了 centerX 并为左右按钮设置了前导和尾随,而且您没有为 UILabel 设置任何宽度限制

 "<NSLayoutConstraint:0x600002d16990 UILabel:0x7fc9eb816f50'Testen'.centerX == _UINavigationBarModernPromptView:0x7fc9ebe27780.centerX   (active)>",
"<NSLayoutConstraint:0x600002d15c20 UILabel:0x7fc9eb816f50'Testen'.leading >= UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'.leading   (active)>",
"<NSLayoutConstraint:0x600002d10ff0 '_UITemporaryLayoutWidth' _UINavigationBarModernPromptView:0x7fc9ebe27780.width == 0   (active)>",
"<NSLayoutConstraint:0x600002d16a30 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UINavigationBarModernPromptView:0x7fc9ebe27780 )>"

"<NSLayoutConstraint:0x600002d16990 UILabel:0x7fc9eb816f50'Testen'.centerX == _UINavigationBarModernPromptView:0x7fc9ebe27780.centerX   (active)>",
    "<NSLayoutConstraint:0x600002d15540 UILabel:0x7fc9eb816f50'Testen'.trailing <= UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide'.trailing   (active)>",
    "<NSLayoutConstraint:0x600002d10ff0 '_UITemporaryLayoutWidth' _UINavigationBarModernPromptView:0x7fc9ebe27780.width == 0   (active)>",
    "<NSLayoutConstraint:0x600002d14320 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x6000037571e0'UIViewLayoutMarginsGuide']-(8)-|(LTR)   (active, names: '|':_UINavigationBarModernPromptView:0x7fc9ebe27780 )>"

    "<NSAutoresizingMaskLayoutConstraint:0x60000043be30 h=--& v=--& _UINavigationBarModernPromptView:0x7fb3c6416640.width == 0   (active)>",
    "<NSLayoutConstraint:0x600000421400 UILabel:0x7fb3c66d8bf0'Plant guide'.centerX == _UINavigationBarModernPromptView:0x7fb3c6416640.centerX   (active)>",
    "<NSLayoutConstraint:0x600000421f90 UILabel:0x7fb3c66d8bf0'Plant guide'.trailing <= UILayoutGuide:0x600001e58fc0'UIViewLayoutMarginsGuide'.trailing   (active)>",
    "<NSLayoutConstraint:0x600000421e00 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001e58fc0'UIViewLayoutMarginsGuide']-(20)-|(LTR)   (active, names: '|':_UINavigationBarModernPromptView:0x7fb3c6416640 )>"

要解决此警告,您应该从 UILabel 中删除前导和尾随约束,并设置左按钮前导到 superview 和右按钮尾随到 superview。

或者您可以使用导航栏对象,它有一个标签,您也可以向其添加 barButtonItem。

您可以为 UILabel 添加宽度限制,但是当您 运行 它在其他设备上时,它会在其他设备上发生冲突

这有助于通过提供更具可读性的输出来调试破坏约束问题

  • 为 NSLayoutConstraint 创建一个扩展

extension NSLayoutConstraint {
    override open var description: String {
        let id = identifier ?? ""
        return "Constraint ID: \(id), constant: \(constant)"
    }
}
  • 为故事板中的每个约束设置唯一标识符

  • 您将获得更具可读性的输出,指出哪个约束 ID 被破坏

  • 更改破坏约束的优先级