When using searchable
with an empty text query, entering and exiting a destination view causes the search to dismiss. This does not happen if there is a query in the search bar. On iOS 15, the destination view also auto-dismisses when doing this. This appears to happen on iPhone, iPad, and Mac Catalyst.
Use Case:
I am trying to use both search and navigation in the sidebar. As is shown below, I have a List
of NavigationLink
s that is shown when the user is searching. Entering a link when there is an empty query in the search bar causes the search to dismiss upon exiting the destination view.
Does anyone have any suggestions or workarounds for this issue?
struct ContentView: View {
@State private var query = ""
private var sidebar: some View {
Sidebar()
.searchable(text: $query)
}
private var detail: some View {
Text("Placeholder for nav split detail pane.")
}
var body: some View {
if #available(iOS 16, *) {
NavigationSplitView {
sidebar
} detail: {
detail
}
} else {
NavigationView {
sidebar
detail
}
}
}
}
struct Sidebar: View {
@Environment(\.isSearching) private var isSearching
var body: some View {
if !isSearching {
Text("Sidebar Content")
.navigationTitle("Example")
} else {
List {
NavigationLink {
Text("Detail View")
} label: {
Text("Search Result")
}
}
}
}
}