I am using the .searchable modifier in SwiftUI. Is there anyway to know when the user presses the search key on the keyboard? I know how to do this by using a UIViewRepresentable and searchController. I'm wondering if there is a SwiftUI way of doing it with the .searchable modifier
Add an .onSubmit(of: .search) modifier. Like this:
List {
ForEach(results, id: \.self) { result in
Text(result)
}
}
.searchable(text: $searchText)
.onSubmit(of: .search) {
// Search button tapped. Perform operation here
}
If if works for you, mark as correct answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With