以编程方式触发时突出显示NSStatusItem [英] Highlight NSStatusItem when triggered programmatically

查看:106
本文介绍了以编程方式触发时突出显示NSStatusItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSStatusItem,带有一个附加菜单,该菜单是通过全局热键触发的.如果单击菜单项,则照常显示突出显示,如果使用热键,则不会触发突出显示.有人知道触发精彩片段的方法吗?

I have an NSStatusItem with an attached menu that I'm triggering with a global hotkey. If I click the menu item I get the highlight as usual, if I use the hotkey the highlight isn't triggered. Does anyone know a way to trigger the highlight?

我尝试覆盖视图,并在drawRect中自己绘制视图,但是如果有更好的方法,我希望听到.谢谢!

I've tried overriding the view and drawing it myself in drawRect but if there is a nicer way to do it I'd love to hear. Thanks!

我不想覆盖视图的原因是我必须处理图标定位,单击以激活菜单等.

The reason I don't want to override the view is then I have to handle icon positioning, clicking to activate the menu, etc.

推荐答案

这在使用Xcode 10的macOS 10.13.6中起到了神奇的作用.

This does the magic in macOS 10.13.6 with Xcode 10.

guard let m = statusItem.menu else { return }
statusItem.button?.isHighlighted = true
statusItem.popUpMenu(m)
statusItem.button?.isHighlighted = false

请注意,关闭菜单后,需要最后一行来取消突出显示图标.

Please note that last line is required to de-highlight the icon when the menu gets closed.

这篇关于以编程方式触发时突出显示NSStatusItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆