A practical guide to implementing quick search in SwiftUI using the `.searchable` API combined with `.onKeyPress`, allowing users to start typing immediately without tapping the search field first. The approach differs between iOS (using a hidden off-screen text field) and macOS (using `.focusable()`), and wraps everything in a reusable `QuickSearchViewModifier`. Key implementation details include handling backspace, space, tab, and escape keys, plus an async workaround for repeat key presses. Note: Apple announced native focus support for `.searchable` at WWDC24, making this approach potentially obsolete for iOS 18+ targets.

5m read timeFrom danielsaidi.com
Post cover image
Table of contents
Update: 2024-06-14BackgroundonKeyPress to the rescueCreating a quick search view modifierHandling key pressesDisclaimer