问题
NSStatusItem.popUpMenu
has been deprecated in macOS 10.14, but I can't find a nice alternative.
let m = statusItem.menu!
statusItem.popUpMenu(m) // deprecated
I tried direct pop-up using menu and the button, but it doesn't position properly.
let m1 = m.items.first!
m.popUp(positioning: m1, at: .zero, in: statusItem.button!)
回答1:
Xcode suggests to use menu
property instead of popupMenu
. But once you set the menu
property, every click on the item will only show the menu.
Instead, if you want to control when the menu is shown, say only in response to a right click, then a simple way to manually trigger the menu is by calling performClick
on NSStatusBarButton
in your handler.
statusItem.menu = myMenu
statusItem.button?.performClick(nil)
statusItem.menu = nil
You have to set menu
back to nil, if you want to keep handling clicks yourself.
来源:https://stackoverflow.com/questions/52585275/what-is-alternative-to-nsstatusitem-popupmenu