Search code examples
iosswiftswiftuiios-navigationview

SwiftUI: Hide Navigation Bar on Specific Screens


It seems as though I'm unable to choose which pages I want to hide the Navigation Bar on in swift UI.

I have the following screens:

Main

struct Main: View {
    var body: some View {
        NavigationView {
                Home()
            }
        }
}

Home

struct Home: View {
    var body: some View {
            NavigationLink(destination: Details()) {
                    Text("Go Next")
            }
        // I expect the navigation bar to show up here, and it does
        .navigationBarTitle("Home")
        .navigationBarHidden(false)
    }
}

Details

struct Details: View {
    @Environment(\.presentationMode) var mode: Binding<PresentationMode>

    var body: some View {
            Button(action: { self.mode.wrappedValue.dismiss() }) {
                Text("Go Back")
            }
        // I expect the navigation bar to be hidden here
        // At first, it is hidden. Then, after a second, it pops up 
        // with the title "Details"
        .navigationBarTitle("Details")
        .navigationBarHidden(true)
    }
}

Either I'm doing this wrong (likely), or Apple has some work to do (also likely).


Solution

  • As I saw earlier, something comes wrong, when you use both:

    .navigationBarTitle("some text")
    .navigationBarHidden(true)
    

    in code below there is no navigation bar staff and nothing pops up:

    struct Details: View {
        @Environment(\.presentationMode) var mode: Binding<PresentationMode>
    
        var body: some View {
                Button(action: { self.mode.wrappedValue.dismiss() }) {
                    Text("Go Back")
                }
            .navigationBarHidden(true)
            .navigationBarBackButtonHidden(true) // even no back button
        }
    }