我构建 SwiftUI 项目的方式是否存在导致 .navigationTitle 不显示的问题?

Is there a problem with the way I'm structuring my SwiftUI project that causes .navigationTitle to not appear?

我的 SwiftUI 项目在某个点后拒绝显示导航标题。我使用的导航结构在我见过的任何示例项目中都没有实现过,但它对我来说很有意义并且到目前为止似乎一直有效。我怀疑是因为我使用的是不同的导航结构,所以这是问题的一部分。 .navigationBar在每个页面上都有,但标题不显示。

SettingsView.swift screen

我在 Stack Overflow 和其他方面尝试过很多解决方案。我已经在下面列出的每个页面上尝试了 .navigationBarHidden(false).navigationBarTitle().navigationBarBackButtonHidden(true) 的所有组合,没有任何变化。我还尝试了我能想到的每个位置来放置这些 .navigationBar 修饰符的组合。

最近,我发现了 .toolbar,这也没有任何改变。我怀疑(如下面的代码片段所示)由于 NavigationView 在第一个视图中 (WelcomeUI.swift),我无法将 .navigationBarTitle 放在代码的更深处。

下面是我当前的导航结构,以及每个文件中的一些代码:

WelcomeUI.swift

struct WelcomeUI: View { 
   var body: some View {
      NavigationView {
         VStack {
            //NavigationLink(destination: SignupUI(), label: {
               //Text("Sign Up")
            //}
            NavigationLink(destination: LoginUI(), label: {
               Text("Log In")
            }
         }
      }
   }
}

LoginUI.swift

struct LoginUI: View {
   var body: some View {
      VStack {
         
         NavigationLink(destination: MainUI(), label: { Text("Log In") })
         //Button(action: { ... }
      }
   .navigationBarHidden(false)
   }
}

注意:SignupUI.swift与LoginUI.swift

本质上是一样的

MainUI.swift

struct MainUI: View {
   var body: some View {
      TabView {
         //SpendingView()
            //.tabItem {
               //Image(...)
               //Text("Spending")
            //}
         //SavingView()
            //.tabItem {
               //Image(...)
               //Text("Saving")
            //}
         //AddView()
            //.tabItem {
               //Image(...)
               //Text("Add")
            //}
         //EditView()
            //.tabItem {
               //Image(...)
               //Text("Edit")
            //}
         SettingsView()
            .tabItem {
               //Image(...)
               Text("Settings")
            }
      }
      .navigationBarBackButtonHidden(true)
   }
}

注意:MainUI.swift 中的所有视图结构相同。

SettingsView.swift

struct SettingsView: View {
   var body: some View {
      ZStack {
         Form {
            Section(header: Text("Section Header")) {
               NavigationLink(destination: WelcomeUI()) {
                  Text("Setting Option")
               }
            }
            Section {
               //Button("Log Out") {
                  //self.logout()
               //}
               Text("Log Out")
            }
         }
         .navigationBarTitle("Settings") // This has no effect on code no matter where it is place in SettingsView.swift
      }
   }
}

我还应该注意,只有 MainUI.swift 之后的页面受此影响。 WelcomeUI.swift 和 LoginUI.swift 按预期工作。

看看MainUInavigationTitle。我只是在每个 View 中添加了一个标题来查找正在发生的事情。

import SwiftUI
struct SubSpendingView: View {
    var body: some View {
        ScrollView{
            Text("SubSpendingView")
            
        }.navigationBarTitle("SubSpending"
                             //, displayMode: .inline
        )
    }
}
struct SpendingView: View {
    var body: some View {
        ScrollView{
            Text("SpendingView")
            NavigationLink("subSpending", destination: SubSpendingView())
        }.padding()
    }
}
struct WelcomeUI: View {
    var body: some View {
        NavigationView {
            VStack {
                //NavigationLink(destination: SignupUI(), label: {
                //Text("Sign Up")
                //}
                NavigationLink(destination: LoginUI(), label: {
                    Text("Go to Log In")
                })
            }.navigationTitle(Text("WelcomeUI"))
        }
    }
}
struct SettingsView: View {
    var body: some View {
        VStack{
            ZStack {
                
                
                Form {
                    Section(header: Text("Section Header")) {
                        NavigationLink(destination: WelcomeUI()) {
                            Text("Setting Option")
                        }
                    }
                    Section {
                        //Button("Log Out") {
                        //self.logout()
                        //}
                        Text("Log Out")
                    }
                }
                Button("say-high", action: {print("Hi")})
                // This has no effect on code no matter where it is place in SettingsView.swift
            }
        }//.navigationBarTitle("Settings")
    }
}
struct LoginUI: View {
    var body: some View {
        VStack {
            
            NavigationLink(destination: MainUI(), label: { Text("Log In") })
            //Button(action: { ... }
        }.navigationTitle(Text("LoginUI"))
        .navigationBarHidden(false)
    }
}
struct MainUI: View {
    @State var selectedTab: Views = .adding
    var body: some View {
        
        TabView(selection: $selectedTab) {
            SpendingView()
                .tabItem {
                    Image(systemName: "bag.circle")
                    Text("Spending")
                }.tag(Views.spending)
            //SavingView()
            //.tabItem {
            //Image(...)
            //Text("Saving")
            //}
            Text("Adding View")
                .tabItem {
                    Image(systemName: "plus")
                    Text("Add")
                }.tag(Views.adding)
            Text("Edit View")
                .tabItem {
                    Image(systemName: "pencil")
                    Text("Edit")
                }.tag(Views.edit)
            SettingsView()
                .tabItem {
                    Image(systemName: "gear")
                    Text("Settings")
                }.tag(Views.settings)
        }
        //This overrides the navigationTitle in the tabs
        .navigationBarTitle(Text(selectedTab.rawValue)
                            //, displayMode: .inline
        )
        .navigationBarBackButtonHidden(true)
    }
}
enum Views: String{
    case settings = "Settings"
    case spending = "Spending"
    case adding = "Add"
    case edit = "Edit"
}
struct PaddyNav: View {
    var body: some View {
        WelcomeUI()
            //Wont work
            .navigationTitle(Text("PaddyNav"))
    }
}

struct PaddyNav_Previews: PreviewProvider {
    static var previews: some View {
        PaddyNav()
    }
}