【问题标题】:.navigationTitle aways creates an autolayout-constraint conflict.navigation 标题总是会产生自动布局约束冲突
【发布时间】:2021-03-21 05:20:39
【问题描述】:

环境: Xcode 版本 12.3 (12C33)
版本 12.3 (12C33)

我注意到 NavigationTitle 在更新到 Xcode 12.3 RC 后会破坏自动布局。
这是一个简单的示例(根据评论修改为现代语法):

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack(alignment: .center, spacing: 10) {
                Text("Hello, world!")
                    .padding()
            }.navigationTitle(Text("Greetings World!"))
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

这是我在运行它时得到的:

2020-12-09 11:42:19.994389-0800 UICheck[26092:799713] [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:0x60000170e1c0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x7feb5ac096e0]-(6)-[_UIModernBarButton:0x7feb5ac06db0'Greetings World!']   (active)>",
    "<NSLayoutConstraint:0x60000170e210 'CB_Trailing_Trailing' _UIModernBarButton:0x7feb5ac06db0'Greetings World!'.trailing <= _UIButtonBarButton:0x7feb5ac066a0.trailing   (active)>",
    "<NSLayoutConstraint:0x60000170ef80 'UINav_static_button_horiz_position' _UIModernBarButton:0x7feb5ac096e0.leading == UILayoutGuide:0x600000d5db20'UIViewLayoutMarginsGuide'.leading   (active)>",
    "<NSLayoutConstraint:0x60000170efd0 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x7feb5ac066a0]-(0)-[UILayoutGuide:0x600000d5da40'UINavigationBarItemContentLayoutGuide']   (active)>",
    "<NSLayoutConstraint:0x60000170ae90 'UINavItemContentGuide-trailing' UILayoutGuide:0x600000d5da40'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x7feb5ae185f0.trailing   (active)>",
    "<NSLayoutConstraint:0x60000170f750 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x7feb5ae185f0.width == 0   (active)>",
    "<NSLayoutConstraint:0x60000170b250 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x600000d5db20'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UINavigationBarContentView:0x7feb5ae185f0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000170e1c0 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x7feb5ac096e0]-(6)-[_UIModernBarButton:0x7feb5ac06db0'Greetings World!']   (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 Bug。
由于我正在使用提供的 .navigationBarTitle 视图,我该如何解决这个问题?

【问题讨论】:

  • 那真的是14.3的缺陷,提交反馈给苹果吧。
  • 我刚刚做了:我将代码连同您的更改一起发送给了 Apple。

标签: swiftui uinavigationbar swiftui-14.3-defect


【解决方案1】:

已针对 Xcode 13 更新

解决SwiftUI NavigationView的LayoutConstraints错误,使其符合这种风格:

.navigationViewStyle(StackNavigationViewStyle()) 在 Xcode 12 上

.navigationViewStyle(.stack) 在 Xcode 13 上

struct ContentView: View {

  var body: some View {
    NavigationView {
      VStack(alignment: .center, spacing: 10) {
        Text("Hello world!")
          .padding()
      }
      .navigationTitle("Greetings World!")
      .navigationBarTitleDisplayMode(.inline)
    }
    .navigationViewStyle(.stack)
  }
}

【讨论】:

  • 这并没有解决我的警告,它仍然发生。原来.navigationViewStyle(StackNavigationViewStyle()) 需要添加到 NavigationView,而不是添加到其中的视图。
  • 代码已针对 Xcode 13 进行了更新,并针对 Xcode 12 进行了修复,在控制台中不显示日志错误。
  • 在 Simulator iPhone 上使用 Xcode 13.2.1 和 iOS 15、15.2,再次出现很多约束冲突。 iOS 14 模拟器上没有冲突。与 21 年 7 月 26 日相同的代码。
猜你喜欢
  • 1970-01-01
  • 2021-08-27
  • 2013-04-07
  • 2017-11-03
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
相关资源
最近更新 更多