Where the search field should attempt to be placed based on the containing view hierarchy.
prompt
A key for the localized prompt of the search field which provides users with guidance on what to search for.
suggestions
A view builder that produces content that populates a list of suggestions.
Discussion
議論
Use this modifier to create a user interface appropriate for searching.
Wrapping a navigation view results in a column of the navigation view displaying a search field. On iOS and iPadOS, the first or second column displays the search field in a double, or triple column navigation view respectively. On macOS, the search field is placed in the trailing-most position of the toolbar.
var body: someView {NavigationView {PrimaryView()SecondaryView()Text("Select a primary and secondary item") } .searchable(text: $text)}
On iOS, iPadOS, or watchOS, wrapping a specific column of a navigation view results in that column displaying a search field.
You can query the isSearching property to adjust the view hierarchy within the searchable modifier. You can also provide search suggestions using the suggestions parameter of searchable modifiers.
Use the searchCompletion(_:) modifier to associate strings to the views provided to the suggestions view. The system uses these strings to replace the partial text being currently edited of the associated search field. If a view has no completion modifier, it’s displayed as is.
If viewModel.suggestedSearches begins as an empty array, then the suggestions aren’t initially displayed. When the array becomes populated, the suggestions are presented. Note that the suggestions may be dismissed based on a user’s action, like moving the window of the app on macOS for example.
You can associate an action to be invoked upon submission of the current search query by using an onSubmit(of:_:) modifier in conjunction with the a searchable modifier.