NOTE: this question is not about how to use .searchable or how to filter a List.
I am using the following view to search an external database:
struct SearchDatabaseView: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.isSearching) private var isSearching: Bool
@State private var searchText: String = ""
@State private var searchResults: [Record] = []
var body: some View {
NavigationStack {
List(searchResults, id: \.self) { record in
/// display results here
}
.navigationTitle("Search Database")
.toolbar {
Button(action: {
dismiss()
}) {
Text("Done")
}
}
.overlay {
if isSearching {
ProgressView("Searching Database...")
}
}
}
.searchable(text: $searchText)
.disableAutocorrection(true)
.onSubmit(of: .search) {
searchDatabase()
}
}
}
Everything works, except the progress view is not showing. I tried putting the .overlay modifier after .onSubmit, but still it doesn't show.
What am I missing, is that not the proper use of isSearching ?
Try this approach, where two views are used (like the docs examples) to perform the search and dismissal using
dismissSearchand display theProgressView.This is just an example code, see the docs at: https://developer.apple.com/documentation/swiftui/managing-search-interface-activation for more comprehensive info and examples.
EDIT-1:
To cater for your new question, I would do away with the
isSearchingthing. Use a "normal" variable and implement a simple but effective code structure, such as in this example code: