ios - SwiftUI NavigationView navigationBarTitle LayoutConstraints 问题

标签 ios swift swiftui

这可能与最近发布的 XCode 新 (12.3) 版本有关,但我有一个非常简单的 SwiftUI View :

import SwiftUI

struct HomeView: View {
    var body: some View {
        NavigationView {
            Text("Text")
                .navigationBarTitle("My Title")
        }
    }
}
我在控制台中收到了这个警告:
2020-12-15 18:25:06.506062-0800 Shopmatic[46177:9585655] [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:0x600003636d00 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x7faf15d0dc30]-(6)-[_UIModernBarButton:0x7faf15c17500'Your Lists']   (active)>",
    "<NSLayoutConstraint:0x600003636d50 'CB_Trailing_Trailing' _UIModernBarButton:0x7faf15c17500'Your Lists'.trailing <= _UIButtonBarButton:0x7faf15c16140.trailing   (active)>",
    "<NSLayoutConstraint:0x600003631e50 'UINav_static_button_horiz_position' _UIModernBarButton:0x7faf15d0dc30.leading == UILayoutGuide:0x600002c18ee0'UIViewLayoutMarginsGuide'.leading   (active)>",
    "<NSLayoutConstraint:0x600003631ea0 'UINavItemContentGuide-leading' H:[_UIButtonBarButton:0x7faf15c16140]-(0)-[UILayoutGuide:0x600002c18e00'UINavigationBarItemContentLayoutGuide']   (active)>",
    "<NSLayoutConstraint:0x600003617160 'UINavItemContentGuide-trailing' UILayoutGuide:0x600002c18e00'UINavigationBarItemContentLayoutGuide'.trailing == _UINavigationBarContentView:0x7faf15e10000.trailing   (active)>",
    "<NSLayoutConstraint:0x600003632580 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x7faf15e10000.width == 0   (active)>",
    "<NSLayoutConstraint:0x600003617520 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x600002c18ee0'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UINavigationBarContentView:0x7faf15e10000 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600003636d00 'BIB_Trailing_CB_Leading' H:[_UIModernBarButton:0x7faf15d0dc30]-(6)-[_UIModernBarButton:0x7faf15c17500'Your Lists']   (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.
这个简单的例子看起来不错,但是当我做一些更复杂的事情时:
import SwiftUI

struct ListDetailView: View {
    var list: List
    
    var body: some View {
        NavigationView {
            Text("Detail View")
            .navigationBarTitle("Detail View Title")
            .navigationBarTitleDisplayMode(.large)
            .navigationBarItems(
                trailing:
                    Button(action: {
                        print("Button Pressed")
                    }) {
                        Image(systemName: "ellipsis")
                }
            )
        }
    }
}
导航标题区域布局都搞砸了:
enter image description here

最佳答案

它与 NavigationBarTitle 无关. . navigationTitle不被弃用。似乎整个问题主要在于新的 Xcode 12.4 更新,即使是简单的代码:

var body: some View {
    NavigationView{
        Text("Hopefully will work this time")
            .navigationTitle("Error with constains")
    }
}
添加 .navigationViewStyle(StackNavigationViewStyle())修复问题
var body: some View {
    NavigationView{
        Text("Yes it does!")
            .navigationTitle("Wow it works")
    }
    .navigationViewStyle(StackNavigationViewStyle())
}

关于ios - SwiftUI NavigationView navigationBarTitle LayoutConstraints 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65316497/

相关文章:

swift - 如何在 tvos 中的 TabBar 项目上全屏播放视频

swift - 使用枚举快速存储当前状态类

swift - 添加枚举后失去了对 Codable 的一致性

SwiftUI 拖放文件

ios - NavigationLink 只能工作一次

ios - ios11如何防止录屏

ios - 解析中的投票实现

ios - Swift4:如何解析 "Fatal error: Can' t form Range with upperBound < lowerBound”?(UI 中的持续时间延迟)

iOS mkmap - 放大/缩小时 map 跳跃的动画注释

iOS Swift UILabel 将两行显示为单行