What is alternative to NSStatusItem.popUpMenu?

痞子三分冷 提交于 2020-01-01 09:48:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!