SearchSuggestionsPlacement

The ways that SwiftUI displays search suggestions.

StructureiOS 16.0+macOS 13.0+tvOS 16.0+watchOS 9.0+SearchSuggestionsPlacement in SwiftUI

Declaration

React
type SearchSuggestionsPlacement = SwiftOpaque<'SearchSuggestionsPlacement'>;
Swift
struct SearchSuggestionsPlacement

Overview

You can influence which modes SwiftUI displays search suggestions for by using the View.searchSuggestions(_:for:) modifier:

enum FruitSuggestion: String, Identifiable {
    case apple, banana, orange
    var id: Self { self }
}

@State private var text = ""
@State private var suggestions: [FruitSuggestion] = []

var body: some View {
    MainContent()
        .searchable(text: $text) {
            ForEach(suggestions) { suggestion in
                Text(suggestion.rawValue)
                    .searchCompletion(suggestion.rawValue)
            }
            .searchSuggestions(.hidden, for: .content)
        }
}

In the above example, SwiftUI only displays search suggestions in a suggestions menu. You might want to do this when you want to render search suggestions in a container, like inline with your own set of search results.

You can get the current search suggestion placement by querying the EnvironmentValues.searchSuggestionsPlacement environment value in your search suggestions.

From Apple’s SwiftUI documentation. Code samples there are Swift.

See also

More in Navigation.