I would like to have a contextual menu that shows search results as text is entered in a search field. This is an image of the default mail app in OS X that does this. I know how to filter an array of strings according to the search request of the user, but I do not know how to display it this way. I am using Swift and for a Cocoa application. Any help is appreciated.
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- 现在使用swift开发ios应用好还是swift?
- Visual Studio Code, MAC OS X, OmniSharp server is
- Using if let syntax in switch statement
- xcode 4 garbage collection removed?
- IntelliJ IDEA can't open projects or add SDK o
- Automator: How do I use the Choose from List actio
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
Building from the previous answer, here is a simple Swift 3 class which you can use to automatically handle recent searches. You can add it as a custom class in your storyboard, or directly. It will look like this:
You need to make an NSMenu Item with special tags that are placeholders so the search field knows where to put recent items, the clear action, etc.
Look at documentation for searchMenuTemplate, and the Tags : NSSearchFieldRecentsMenuItemTag etc.
Basically, make a contextual menu in IB. Drag your search field to use that menu as the searchMenuTemplate, and then populate menu items with the tags you want for clear, recent items, etc.
NSSearchField
searchMenuTemplate
(NSMenu) contains Menu Items (NSMenuItem) with specific tags used by the Search Field to populate the Menu.The
recentSearches
Array here is just to pass additionalString
used in complement of Recents Search strings and is not required (I thought that it was to store recent search but no). NSSearchField also clear this Array when the user clears Recents Search.You can also configure a Menu with category, more info here: Configuring a Search Menu — Apple Developer
Example: