简体   繁体   中英

Detect up/down arrow keys in UISearchBar so keyboard can be used to highlight search results

How can I detect presses of the up/down arrow keys on the keyboard when my UISearchBar is the first responder? I want users to be able to use the keyboard to select items in the list of search results.

I've tried adding key commands for the up/down arrows in the view controller that contains the UISearchBar, but the methods never get called when the UISearchBar is the first responder.

This functionality is possible in Safari, Mail, Photos, and other native apps - hopefully Apple is not using some sort of private API there.

Of course I found the answer right after posting. You have to set wantsPriorityOverSystemBehavior on the UIKeyCommand to get the command to work when a text field is focused.

https://developer.apple.com/videos/play/wwdc2021/10260/?time=425

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM