首页 > 解决方案 > 为什么 .navigationTitle 接缝在设备上调试时抛出 UIViewAlertForUnsatisfiableConstraints 警告?

问题描述

如果我运行此代码:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Text")
                .navigationTitle("My Title")
        }
    }
}

我在使用 iPhone 进行调试时收到以下警告:

2021-03-15 18:00:08.023556+0100 Trial2[373:7602] [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:0x283874dc0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x103817930]-(6)-[_UIModernBarButton:0x102f2e3d0'My Title']   (active)>",
    "<NSLayoutConstraint:0x283874e10 'CB_Trailing_Trailing' _UIModernBarButton:0x102f2e3d0'My Title'.trailing <= BackButton.trailing   (active, names: BackButton:0x102f2d9a0 )>",
    "<NSLayoutConstraint:0x283875b30 'UINav_static_button_horiz_position' _UIModernBarButton:0x103817930.leading == UILayoutGuide:0x282245f80'UIViewLayoutMarginsGuide'.leading   (active)>",
    "<NSLayoutConstraint:0x283875b80 'UINavItemContentGuide-leading' H:[BackButton]-(0)-[UILayoutGuide:0x282245ea0'UINavigationBarItemContentLayoutGuide']   (active, names: BackButton:0x102f2d9a0 )>",
    "<NSLayoutConstraint:0x283858b90 'UINavItemContentGuide-trailing' UILayoutGuide:0x282245ea0'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x102f220c0.trailing   (active)>",
    "<NSLayoutConstraint:0x283876300 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x102f220c0.width == 0   (active)>",
    "<NSLayoutConstraint:0x283858a00 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x282245f80'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UINavigationBarContentView:0x102f220c0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283874dc0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x103817930]-(6)-[_UIModernBarButton:0x102f2e3d0'My Title']   (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.

我在这里看到了一个类似的问题,但问题是它.navigationBarTitle已被弃用。就我而言,我正在使用.navigationTitle. 它也被弃用了吗(我怀疑它,因为文档没有这么说)?我是否以错误的方式使用它?这是Xcode的错误吗?或者只是这是正常的,我不应该担心这些警告?

PS:我是 Swift 和 SwiftUI 编程的新手


编辑:@Andrew 发现了一些似乎可以解决问题的方法(将修饰符添加.navigationViewStyle(StackNavigationViewStyle())NavigationView)。然而,我不明白这是否是解决我的代码中明确定义的问题的正确解决方案,还是解决 Xcode 误报/错误的技巧。我希望有人向我解释为什么这段代码似乎可以解决我的问题。

我认为这不是.navigationViewStyle(StackNavigationViewStyle())正确的解决方案,因为问题是在遵循Apple 的教程时发生的。我认为如果错误是意料之中的,Apple 会知道并将这一行放在教程中。所以我更容易相信这是一个 Xcode 14 错误。


回顾一下,我的问题是:

  1. Xcode 发出的警告是预期的吗?或者它是一个错误?
  2. 为什么要.navigationViewStyle(StackNavigationViewStyle())解决问题?
  3. 什么时候这个警告真的是一个问题?(我运行它时看不到任何错误)

标签: swiftui

解决方案


推荐阅读