4

I am having trouble hiding the navigation bar in case of multiple navigation views. I want navigation bars to be present on first and second screen but not on the third one.

struct FirstView: View { init() { UINavigationBar.appearance().backgroundColor = UIColor.green } var body: some View { NavigationView { NavigationLink(destination: SecondView()) { Text("Second View") }.navigationBarTitle("First View") } } } // Second View struct SecondView: View { var body: some View { NavigationLink(destination: ThirdView()) { Text("Third View") } } } // Third View struct ThirdView: View { var body: some View { Text("Welcome") .navigationBarTitle("") .navigationBarHidden(true) } } 

I tried hiding the navigation bar on third screen with the above code, but it doesn't work :(

It doesn't hide the navigation bar

1
  • 1
    You should only have one NavigationView per navigation stack. Commented Jun 30, 2020 at 11:55

1 Answer 1

2

If you want to hide navigation bar completely at third view here is possible approach. (Note: btw in one view hierarchy there must be only one NavigationView, so another one in ThirdView is not needed)

Tested with Xcode 11.4 / iOS 13.4

class HideBarViewModel: ObservableObject { @Published var isHidden = false } struct FirstView: View { @ObservedObject var vm = HideBarViewModel() init() { UINavigationBar.appearance().backgroundColor = UIColor.green } var body: some View { NavigationView { NavigationLink(destination: SecondView()) { Text("Second View") }.navigationBarTitle("First View") .navigationBarHidden(vm.isHidden) }.environmentObject(vm) } } // Second View struct SecondView: View { var body: some View { NavigationLink(destination: ThirdView()) { Text("Third View") } } } // Third View struct ThirdView: View { @EnvironmentObject var vm: HideBarViewModel var body: some View { Text("Welcome") .onAppear { self.vm.isHidden = true } } } 
Sign up to request clarification or add additional context in comments.

1 Comment

How will it help me in hiding navigation bar for the third screen? The solution you suggested simply adds a title to second screen as well. Tried your solution but it doesn't solve what i am trying to achieve :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.