I have a problem when pushing to a new view with a search bar active.
In the pushed view I want the navigation bar to be hidden. It works in all cases except when the search field is active on the pushing view AND the navigation style is StackNavigationViewStyle.
In the example project below if you select a row, the new view is pushed and the navigation bar is hidden as expected. However, if you first select the search bar to make it active and then press a row, the navigation bar is no longer hidden on the pushed view.
Removing the StackNavigationViewStyle, everything will work fine however I need to have StackNavigationViewStyle.
struct ContentView: View {
@State var searchString = ""
var body: some View {
NavigationView {
List {
NavigationLink {
PushedView()
} label: {
Text("Press Me")
}
}
.listStyle(PlainListStyle())
.searchable(text: $searchString)
.navigationTitle("First View")
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct PushedView: View {
@Environment(.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
Button {
self.presentationMode.wrappedValue.dismiss()
} label: {
Text("Pop View")
}
.navigationTitle("Second View")
.navigationBarHidden(true)
}
}
2
Answers
This might be a solution for now:
I accidentally find a way to make this work and I don’t understand why.
In
PushedView
, instead of calling.navigationBarHidden(true)
, you do this instead:false
. Let’s call that variableisBarHidden
.navigationBarHidden(isBarHidden)
OnAppear
, setisBarHidden = true
The full
PushedView
should look something like this: