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
Question posted in Xcode
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
2
Answers
This is still beta in macOS 12 (but not i/iPad/tvOS 15) as I write this so not really for this forum, however…
searching has an instance property:
var isSearching: Bool { get }
which can be passed into a child view using:
@Environment(.isSearching) var isSearching
and subsequently interrogated from that
var
Look up the Apple documentation
Add an .onSubmit(of: .search) modifier. Like this:
If if works for you, mark as correct answer.