snitron
snitron

Reputation: 71

How to hide NavigationBar in SwiftUI for all types of devices?

I have a view hierarchy like this: SplashView -> MainView.

SplashView doesn't need to have NavigationBar, so I disable it:

    NavigationView {
        VStack { /* some stuff */}        
    }    
         .navigationBarTitle("", displayMode: .inline)
         .navigationBarHidden(true)
         .edgesIgnoringSafeArea([.top, .bottom])

In MainView I need an inline Navigation Bar

 NavigationView {
        ZStack{ /* some stuff */ } 
        }.navigationBarTitle("TEST TITLE", displayMode: .inline)
            .navigationBarHidden(false)
            .navigationBarBackButtonHidden(true)
            .navigationBarItems(leading: EmptyView())
            .navigationBarItems(trailing: HStack {
                           Button("Test1") { print("test1")

                           }
                           Button("Test2") {
                               print("test2")
                   }
                       })
        }

As result, I have this on iPhone 11:

MainView iPhone 11

And on iPhone SE 2nd gen. (simillar on iPhone 8 etc.):

MainView on iPhone SE 2nd gen.

And if you manage, you can click "Back button" and go to the SplashView!

Why it works like this and what I should do to fix it?

Upvotes: 0

Views: 348

Answers (1)

Alexander Gaidukov
Alexander Gaidukov

Reputation: 759

I think, the problem is you have the NavigationView as a root view for both screens. In this case you push the NavigationView in another NavigationView. So, you need the only one NavigationView in your project. So, change the body property of your MainView by removing the NavigationView

ZStack{ /* some stuff */ } 
    }.navigationBarTitle("TEST TITLE", displayMode: .inline)
        .navigationBarHidden(false)
        .navigationBarBackButtonHidden(true)
        .navigationBarItems(leading: EmptyView())
        .navigationBarItems(trailing: HStack {
                       Button("Test1") { print("test1")

                       }
                       Button("Test2") {
                           print("test2")
               }
                   })

Upvotes: 1

Related Questions