lcj
lcj

Reputation: 1797

SwiftUI use searchable modifier with an enter/search button

I want to use the searchable modifier in swiftui but am calling a webapi and don't want to do so on every character. Most of the examples I have seen, like this one and this one search on each character (or potentially a certain number of characters) but I want to search only on enter/search key. I have not seen an example of this. Has anybody done this before?

I'm new to SwiftUI, so there may be a simple answer I'm missing.

Upvotes: 6

Views: 3040

Answers (1)

lorem ipsum
lorem ipsum

Reputation: 29301

You can use .onSubmit(of: .search)

ContentView()
    .searchable(text: $text) {
        MySearchSuggestions()
    }
    .onSubmit(of: .search) {
        fetchResults()
    }

https://developer.apple.com/wwdc21/10176

Upvotes: 11

Related Questions