如何获取 NSStatusItem 的框架
将 NSStatusItem
添加到 Cocoa 中的状态栏后,是否可以获取它的框架?当我的应用程序启动时,我将一个项目添加到系统状态栏,并且想知道它的位置,这是可能的。
Is it possible to get the frame of a NSStatusItem
after I've added it to the status bar in Cocoa? When my app is launched, I am adding an item to the system status bar, and would like to know where it was positioned, is possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
以下似乎有效 - 我见过类似的 iOS 应用程序解决方案,据说它们允许提交到应用程序商店,因为您仍在使用标准 SDK 方法。
The following seems to work - I have seen similar solutions for iOS applications and supposedly they permit submission to the app store because you are still using standard SDK methods.
在 10.10 中,
NSStatusItem
有一个button
属性,用于在不设置自定义视图的情况下获取状态项位置。With 10.10,
NSStatusItem
has abutton
property that be used to get the status item position without setting a custom view.您可以在 swift 中使用
statusItem.button.superview?.window?.frame
You can use
statusItem.button.superview?.window?.frame
in swift如果您在状态项上设置了自定义视图:
否则我认为不可能使用可用且已记录的 API。
编辑:合并评论。
If you have set a custom view on the status item:
Otherwise I don't think it's possible using the available and documented APIs.
Edit: Incorporated comments.
无需任何私有 API 即可完成此操作。这是 NSScreen 的类别。这使用图像分析来定位菜单栏上状态项的图像。幸运的是,计算机速度非常快。 :)
只要您知道状态项的图像是什么样子,并且可以将其作为 NSImage 传入,此方法就应该找到它。
适用于深色模式和常规模式。请注意,您传入的图像必须是黑色的。彩色图像可能效果不太好。
It's possible to do this without any private API. Here's a category for NSScreen. This uses image analysis to locate the status item's image on the menu bar. Fortunately, computers are really fast. :)
As long as you know what the status item's image looks like, and can pass it in as an NSImage, this method should find it.
Works for dark mode as well as regular mode. Note that the image you pass in must be black. Colored images will probably not work so well.
你可以像这样破解窗口 ivar :
这对于没有自定义视图的状态项很有用。
在 Lion 上测试
you can hack the window ivar like this :
This is useful for status items without a custom view.
Tested on Lion