Overloads
2 ways to call compose().searchScopes(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().searchScopes<V>(
scope: Binding<V>,
options: {
activation: SwiftUI.SearchScopeActivation;
scopes: ReactNode;
}
)nonisolated func searchScopes<V, S>(
_ scope: Binding<V>,
activation: SearchScopeActivation,
@ContentBuilder _ scopes: () -> S
) -> some View
where V : Hashable, S : ViewParameters
scope*Binding<V>The active scope of the search field.
options.activation*SwiftUI.SearchScopeActivationThe activation style of the search field's scopes.
options.scopes*ReactNodeA content builder that represents the scoping options SwiftUI uses to populate a
Picker.
Discussion
To enable people to narrow the scope of their searches, you can create a type that represents the possible scopes, and then create a state variable to hold the current selection. For example, you can scope the product search to just fruits or just vegetables:
enum ProductScope {
case fruit
case vegetable
}
@State private var scope: ProductScope = .fruitProvide a binding to the scope, as well as a view that represents each scope:
ProductList()
.searchable(text: $text, tokens: $tokens) { token in
switch token {
case .apple: Text("Apple")
case .pear: Text("Pear")
case .banana: Text("Banana")
}
}
.searchScopes($scope) {
Text("Fruit").tag(ProductScope.fruit)
Text("Vegetable").tag(ProductScope.vegetable)
}SwiftUI uses this binding and view to add a Picker below the search
field. In iOS, macOS, and tvOS, the picker appears below the search
field when search is active. To ensure that the picker operates
correctly, match the type of the scope binding with the type of each
view's tag. Then condition your search on the current value of the
scope state property.
By default, the appearance of scopes varies by platform:
- In iOS and iPadOS, search scopes appear when someone enters text into the search field and disappear when someone cancels the search.
- In macOS, search scopes appear when SwiftUI presents search and disappear when someone cancels the search.
However, you can use the activation parameter with a value of
SearchScopeActivation.onTextEntry or
SearchScopeActivation.onSearchPresentation to configure this
behavior:
.searchScopes($scope, activation: .onSearchPresentation) {
Text("Fruit").tag(ProductScope.fruit)
Text("Vegetable").tag(ProductScope.vegetable)
}For more information about using searchable modifiers, see doc:Adding-a-search-interface-to-your-app.
See also
More in Navigation.
- assistiveAccessNavigationIconConfigures the view's icon for purposes of navigation.
- asyncImageURLSessionA modifier that adds a URL session for asynchronous images contained in the view to use when fetching image data.
- contentToolbarPopulates the toolbar of the specified content view type with the views you provide.
- defaultAdaptableTabBarPlacementSpecifies the default placement for the tabs in a tab view using the adaptable sidebar style.
- defaultTabBarPlacementSpecifies the preferred placement for the tabs of a TabView in the TabViewStyle.sidebarAdaptable style on platforms where the tab bar cannot adapt between different representations, and only one representation can be shown.
- navigationBarBackButtonHiddenHides the navigation bar back button for the view.
- navigationBarTitleDisplayModeConfigures the title display mode for this view.
- navigationDestinationAssociates a destination view with a presented data type for use within a navigation stack.
- navigationDocumentConfigures the view's document for purposes of navigation.
- navigationLinkIndicatorVisibilityConfigures whether navigation links show a disclosure indicator.
- navigationSplitViewColumnWidthSets a fixed, preferred width for the column containing this view.
- navigationSubtitleConfigures the view's subtitle for purposes of navigation, using a localized string resource.