Home > Software design >  how to hide navigation bar on particular screen in SwiftUI?
how to hide navigation bar on particular screen in SwiftUI?

Time:09-21

I am navigating from one screen to another on previous screen i am hiding navigation bar but on second screen I want to show navigation bar but in mu case its hiding on second screen also.

navigation bar hidden on first screen as

.navigationBarHidden(true)

How can i show navigation bar on second screen? (I am NOT using navigation view on either screen)

My First parent screen has

            NavigationView {
//design
}
.navigationBarTitle("ABCScreen", displayMode: .inline)
                .navigationViewStyle(StackNavigationViewStyle())

Thank you for help.

CodePudding user response:

The possible approach to hide navigation bar in root view and show in child subviews.

struct FirstNavigationView: View {
    @State private var hideBar = true // <<  hide state
    var body: some View {
        NavigationView {
            VStack {
                Text("FirstView")
                Divider()
                NavigationLink("Goto Child", destination: NextChildView(index: 1))
                 .simultaneousGesture(TapGesture().onEnded {
                    self.hideBar = false     // << show
                 })
            }
            .navigationBarHidden(hideBar)
        //    .navigationBarTitle("Back to Root") // << choice 
            .onAppear {
                self.hideBar = true  // << hide on back
            }
        }
    }
}

The only needed modifications is in root view.

CodePudding user response:

For your first screen you can add .navigationBarHidden(true) inside your NavigationView to hide it and false on second screen to unhide.

FIRST SCREEN:

NavigationView{
        Button(action: {}, label: {
            Text("Button")
        })
        .navigationBarHidden(true) //This flag will hide your navigationBar
    }

SECOND SCREEN:

NavigationView{
        Button(action: {}, label: {
            Text("Button")
        })
        .navigationBarHidden(false) //This flag will unhide your navigationBar
    }
  • Related