如何始终从任何视图直接使用NavigationView隐藏导航栏?

huangapple go评论65阅读模式
英文:

How hide navigation bar always back from any view directly using NavigationView?

问题

我正在使用xcode-14.2和最低目标版本14。我有三个视图 ContentViewWelcomeFundTransfer。以下是我的情况。

  1. ContentView - 加载第一个视图,navigationBarHidden 正常工作。当单击 Welcome 页按钮时,它转到 Welcome 页。
  2. Welcome 视图 - 当单击 Fund Transfer 按钮时,它转到 FundTransfer 视图。
  3. FundTransfer - 当单击 Log out 按钮时,它转到 ContentView

流程如下:ContentView -> FundTransfer -> ContentView

问题:当从 FundTransfer 视图返回到 ContentView 时,导航栏会显示出来。这意味着从 FundTransfer 视图返回到 ContentView 时会显示导航栏,而在一开始时是隐藏的。

如何始终从任何视图直接返回到 ContentView 时隐藏导航栏?

以下是我的代码:

ContentView:

struct ContentView: View {
    
    @State private var showWelcome = false
    @State var isNavigationBarHidden: Bool = true
    
    var body: some View {
        NavigationView {
            VStack {
                ScrollView {
                    VStack(alignment: .customCenter,spacing: 0){
                        
                        VStack {
                            SubmitButton(action: {
                                self.showWelcome = true
                            }) {
                                Text("Welcome page")
                            }
                        }
                        
                        NavigationLink(destination: Welcome(), isActive: $showWelcome) { EmptyView() }
                    }
                }
            }
            
            .navigationBarTitle("") //this must be empty
            .navigationBarHidden(true)
            .navigationBarBackButtonHidden(true)
            
        }
    }
}

Welcome View:

struct Welcome: View {
    @State private var showFundTransfer = false
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        VStack {
            ScrollView {
                VStack(alignment: .customCenter,spacing: 0){
                    VStack {
                        SubmitButton(action: {
                            showFundTransfer = true
                        }) {
                            Text("Fund Transfer")
                        }
                    }
                    
                    NavigationLink(destination: FundTransfer(), isActive: $showFundTransfer) { EmptyView() }
                }
            }
            
            .navigationBarHidden(true)
        }
        
    }
}

FundTransfer View:

struct FundTransfer: View {
    @State var isNavigationBarHidden: Bool = true
    @State private var logon = false
    var body: some View {
        
        VStack {
            ScrollView {
                VStack(alignment: .customCenter,spacing: 0){
                    SubmitButton(action: {
                            self.logon = true
                       
                    }) {
                        Text("Log out")
                    }
                }
            }
            
            NavigationLink(destination: ApplicationSwitcher(), isActive: $logon) { EmptyView() }.opacity(0)
        }
        .navigationBarHidden(true)
        
    }
}

请帮助我。

英文:

I am using xcode-14.2 & minimum target version 14. I have three views ContentView, Welcome & `FundTransfer. Here is my case.

  1. ContentView - Load first view & navigationBarHidden is working. When Welcome page button click it goes to Welcome page
  2. Welcome view - When Fund Transfer button is clicked, it goes to FundTransfer view
  3. FundTransfer - when Log out button is clicked, it goes to ContentView

It goeslike: ContentView-> FundTransfer-> ContentView

Problem: When it goes from FundTransfer view to ContentView it shows navigationBar. That means when back from FundTransfer view to ContentView shows navigationBar which was hidden at the first.

How do I hide navigation bar always back from any view directly to ContentView?

Here is my code:
ContentView:

    struct ContentView: View {
        
        @State private var showWelcome = false
        @State var isNavigationBarHidden: Bool = true
        
        var body: some View {
            NavigationView {
                VStack {
                    ScrollView {
                        VStack(alignment: .customCenter,spacing: 0){
                            
                            VStack {
                                SubmitButton(action: {
                                    self.showWelcome = true
                                }) {
                                    Text("Welcome page")
                                }
                            }
                            
                            NavigationLink(destination: Welcome(), isActive: $showWelcome) { EmptyView() }
                        }
                    }
                }
                
                .navigationBarTitle("") //this must be empty
                .navigationBarHidden(true)
                .navigationBarBackButtonHidden(true)
                
            }
        }
    }

Welcome View:

    struct Welcome: View {
        @State private var showFundTransfer = false
        @Environment(\.presentationMode) var presentationMode
        var body: some View {
            VStack {
                ScrollView {
                    VStack(alignment: .customCenter,spacing: 0){
                        VStack {
                            SubmitButton(action: {
                                showFundTransfer = true
                            }) {
                                Text("Fund Transfer")
                            }
                        }
                        
                        NavigationLink(destination: FundTransfer(), isActive: $showFundTransfer) { EmptyView() }
                    }
                }
                
                .navigationBarHidden(true)
            }
            
        }
    }

FundTransfer View:

        struct FundTransfer: View {
        @State var isNavigationBarHidden: Bool = true
        @State private var logon = false
        var body: some View {
            
            VStack {
                ScrollView {
                    VStack(alignment: .customCenter,spacing: 0){
                        SubmitButton(action: {
                                self.logon = true
                           
                        }) {
                            Text("Log out")
                        }
                    }
                }
                
                NavigationLink(destination: ApplicationSwitcher(), isActive: $logon) { EmptyView() }.opacity(0)
            }
            .navigationBarHidden(true)
            
        }
    }

Please help me..

答案1

得分: 1

在NavigationLink中也添加.navigationBarHidden(true)例如:

NavigationLink(destination: ApplicationSwitcher()
.navigationBarHidden(true), isActive: $logon) { EmptyView() }.opacity(0)
英文:

Add .navigationBarHidden(true) in NavigationLink also for eg:

NavigationLink(destination: ApplicationSwitcher()
.navigationBarHidden(true), isActive: $logon) { EmptyView() }.opacity(0)

答案2

得分: 1

在ContentView的NavigationView的闭包之后,按照下面的方式添加"navigationBarHidden(true)"而不是VStack:

NavigationView {
...
}.navigationBarTitle("")
 .navigationBarHidden(true)
 .navigationBarBackButtonHidden(true)

如何始终从任何视图直接使用NavigationView隐藏导航栏?

英文:

In ContentView add "navigationBarHidden(true)" after the closure of NavigationView instead of VStack as mentioned below:

NavigationView {
...
}.navigationBarTitle("")
 .navigationBarHidden(true)
 .navigationBarBackButtonHidden(true)

如何始终从任何视图直接使用NavigationView隐藏导航栏?

huangapple
  • 本文由 发表于 2023年1月9日 01:47:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定