How to hide SwiftUI NavigationBar
When playing with SwiftUI NavigationView I have came across multiple issues. One of them was how to hide navigation bar on specific view.
Solution
Hide NavigationBar
Using just .navigationBarHidden(true) just doesn't work. For some crippled reason you have to add also .navigationBarTitle("").
struct SomeView: View {
var body: some View {
VStack){
Text("Hello World")
}
.navigationBarTitle("")
.navigationBarHidden(true)
}
}Bonus 1: Hide NavigationBar when app is reopened from Background
struct SomeView: View {
@State var navBarHidden: Bool = true
var body: some View {
VStack){
Text("Hello World")
}
.navigationBarTitle("")
.navigationBarHidden(self.navBarHidden)
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
self.navBarHidden = true
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
self.navBarHidden = false
}
}
}You think you have won this battle? NO. When you close and reopen the app on this view, you will NavigationBar is presented again. I din't find better solution yet, so here is ugly one.
Bonus 2: Get you NavigationBar under control
NavigationView dynamic background color in SwiftUI
SwiftUI Elements
You can find also the whole implementation in my opensource UI kit.