Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when SwiftUI keyboard search button tapped in .searchable search window

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

like image 367
alionthego Avatar asked Oct 30 '25 14:10

alionthego


1 Answers

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.

like image 120
Victor Avatar answered Nov 02 '25 06:11

Victor