Contents

isSearching

A Boolean value that indicates when the user is searching.

Declaration

var isSearching: Bool { get }

Mentioned in

Discussion

You can read this value like any of the other EnvironmentValues, by creating a property with the Environment property wrapper:

@Environment(\.isSearching) private var isSearching

Get the value to find out when the user interacts with a search field that’s produced by one of the searchable modifiers, like searchable(text:placement:prompt:):

struct SearchingExample: View {
    @State private var searchText = ""

    var body: some View {
        NavigationStack {
            SearchedView()
                .searchable(text: $searchText)
        }
    }
}

struct SearchedView: View {
    @Environment(\.isSearching) private var isSearching

    var body: some View {
        Text(isSearching ? "Searching!" : "Not searching.")
    }
}

When the user first taps or clicks in a search field, the isSearching property becomes true. When the user cancels the search operation, the property becomes false. To programmatically set the value to false and dismiss the search operation, use dismissSearch.

See Also

Detecting, activating, and dismissing search