NSStatusItem.popUpMenu的替代品是什么? [英] What is alternative to NSStatusItem.popUpMenu?
问题描述
NSStatusItem.popUpMenu
在macOS 10.14中已被弃用,但我找不到合适的替代方法.
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!)
推荐答案
Xcode建议使用menu
属性而不是popupMenu
.但是一旦设置了menu
属性,每次单击该项目将仅显示菜单.
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.
相反,如果您想控制菜单的显示时间,例如仅在右键单击时说,那么手动触发菜单的一种简单方法是在处理程序中的NSStatusBarButton
上调用performClick
.>
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
如果您想继续自己处理点击,则必须将menu
设置为nil.
You have to set menu
back to nil, if you want to keep handling clicks yourself.
这篇关于NSStatusItem.popUpMenu的替代品是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!