Contents

SearchSuggestionsPlacement

The ways that SwiftUI displays search suggestions.

Declaration

struct SearchSuggestionsPlacement

Overview

You can influence which modes SwiftUI displays search suggestions for by using the 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 searchSuggestionsPlacement environment value in your search suggestions.

Topics

Getting placements

Supporting types

See Also

Making search suggestions