Swiftui Show/Hide Title Issues with Navigationbar

SwiftUI show/hide title issues with NavigationBar

Below is a possible approach to hide navigation bar in root view and show in child subviews. The only needed modifications is in root view.

Tested with Xcode 11.4 / iOS 13.4

demo

Here is a root only, child sub-views are regular and do not require special code for this case. See important notes inline.

struct RootNavigationView: View {
@State private var hideBar = true // << track hide state, and default
var body: some View {
NavigationView {
VStack {
Text("I'm ROOT")
Divider()
NavigationLink("Goto Child", destination: NextChildView(index: 1))
.simultaneousGesture(TapGesture().onEnded {
self.hideBar = false // << show, here to be smooth !!
})
}
.navigationBarHidden(hideBar)
// .navigationBarTitle("Back to Root") // << optional
.onAppear {
self.hideBar = true // << hide on back
}
}
}
}

SwiftUI Navigation Bar Title

It should be inside NavigationView, like

struct ContentView: View {
var body: some View {
NavigationView {
Form {
...
}
.navigationBarTitle(Text("WeSplit")) // << here !!
}
}
}

Navigation Bar hide is not working in SwiftUI

NOTE: (For some reason it works in some cases) SwiftUI requires that you need to .navigationBarTitle for .navigationBarHidden to work properly.

NavigationView {
ScrollView() {
......
}.
.navigationBarTitle("") //this must be empty
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}


Related Topics



Leave a reply



Submit