Search code examples
iosswiftswiftuiswiftui-navigationview

Declare View Name for NavigationView but don't show it swift


I have a view that uses a NavigationLink to navigate to another view. Normally, if the first view has a NavigationTitle, then the back button for the second will show the name. However, I don't want to show a NavigationTitle in the first view. Is there a way to still get the NavigationView to recognize the title of my first view without declaring a title that will show up?


Solution

  • Set .navigationBarHidden(true) on your first view.

    struct ContentView: View {
        var body: some View {
            NavigationView {
                NavigationLink("Open DetailView", destination: DetailView())
                .navigationBarTitle("Home")
                .navigationBarHidden(true)
            }
        }
    }
    
    struct DetailView: View {
        var body: some View {
            Text("This is DetailView")
                .navigationBarTitle("Detail")
        }
    }