DismissSearchAction
An action that can end a search interaction.
Declaration
type DismissSearchAction = { (): Promise<void>; };@MainActor @preconcurrency struct DismissSearchActionOverview
Use the EnvironmentValues.dismissSearch environment value to get the
instance of this structure for a given Environment. Then call the
instance to dismiss the current search interaction. You call the instance
directly because it defines a DismissSearchAction.callAsFunction()
method that Swift calls when you call the instance.
When you dismiss search, SwiftUI:
- Sets
EnvironmentValues.isSearchingtofalse. - Clears any text from the search field.
- Removes focus from the search field.
interacting with a search field.
Use this action to dismiss a search operation based on
another user interaction. For example, consider a searchable
view with a Button that presents more information about the first
matching item from a collection:
struct ContentView: View {
@State private var searchText = ""
var body: some View {
NavigationStack {
SearchedView(searchText: searchText)
.searchable(text: $searchText)
}
}
}
struct SearchedView: View {
var searchText: String
let items = ["a", "b", "c"]
var filteredItems: [String] { items.filter { $0 == searchText.lowercased() } }
@State private var isPresented = false
@Environment(\.dismissSearch) private var dismissSearch
var body: some View {
if let item = filteredItems.first {
Button("Details about \(item)") {
isPresented = true
}
.sheet(isPresented: $isPresented) {
NavigationStack {
DetailView(item: item, dismissSearch: dismissSearch)
}
}
}
}
}The button becomes visible only after the user enters search text that produces a match. When the user taps the button, SwiftUI shows a sheet that provides more information about the item, including an Add button for adding the item to a stored list of items:
private struct DetailView: View {
var item: String
var dismissSearch: DismissSearchAction
@Environment(\.dismiss) private var dismiss
var body: some View {
Text("Information about \(item).")
.toolbar {
Button("Add") {
// Store the item here...
dismiss()
dismissSearch()
}
}
}
}People can dismiss the sheet by dragging it down, effectively
canceling the operation, leaving the in-progress search interaction
intact. Alternatively, people can tap the Add button to store the item.
Because the person using your app is likely to be done with both the
detail view and the search interaction at this point, the button's
closure also uses the EnvironmentValues.dismiss property to dismiss
the sheet, and the EnvironmentValues.dismissSearch property to
reset the search field.
From Apple’s SwiftUI documentation. Code samples there are Swift.
See also
More in Navigation.
- AdaptableTabBarPlacementA placement for tabs in a tab view using the adaptable sidebar style.
- AutomaticNavigationTransitionA style that automatically chooses the appropriate presentation transition for the current context.
- ContentToolbarPlacementA region of the interface that hosts its own toolbar content.
- CrossFadeNavigationTransitionA navigation transition that cross-fades between the appearing view and the disappearing view.
- NavigationBarItemTitleDisplayModeA style for displaying the title of a navigation bar.
- NavigationPathA type-erased list of data representing the content of a navigation stack.
- NavigationPathChainNavigationPath with chained calls: SwiftUI.NavigationPath.append(...).
- NavigationPathCodableRepresentationA serializable representation of a navigation path.
- NavigationSplitViewColumnA view that represents a column in a navigation split view.
- NavigationSplitViewVisibilityThe visibility of the leading columns in a navigation split view.
- NavigationTransitionA type that defines the transition to use when navigating to a view.
- OpenURLActionAn action that opens a URL.