1

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

SettingsView.swift 屏幕

我在 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 按预期工作。

4

1 回答 1

1

看看MainUI navigationTitle。我只是在每个标题中加上一个标题,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()
    }
}
于 2020-12-14T17:48:03.900 回答