如何将图标添加到 NSMenuItem 的右侧
我正在尝试将一些图标添加到菜单项旁边的菜单右侧,最好是合理的。我知道 NSMenuItem 允许您添加菜单项(例如,请参阅 MenuMadness 示例)。我真的在寻找诸如 OS X Wifi 菜单中的安全/信号强度指示器之类的东西。
我见过的一种方法是将项目添加到菜单中,并使用 setView 为每个项目提供一个 NSView。然而,这似乎比应有的更复杂。有没有办法只向菜单添加一个图标/多个图标?
I'm trying to add some icons to the right hand side of a menu next to a menu item, ideally all right justified. I'm aware that NSMenuItem allows you to add menu items (for instance, see the MenuMadness example). I'm really looking for something like the security / signal strength indicators in the OS X Wifi menu.
One approach that I've seen is to add items to the menu, and use setView to provide an NSView for each item. However, that seems to be more complicated than it should be. Is there a way to just add an icon / multiple icons to the menu?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在菜单的左侧,这很简单,甚至可以从 Interface Builder 中完成。
对于右侧,我假设您已经阅读了 Apple 文档 菜单项中的视图。我同意,它比需要的要复杂得多,并且有很多缺点。
您是否考虑过一种巧妙的解决方法?
只需创建没有标题的菜单,并设置一个非常宽的图标图像的图标,包括您的标题和图标。
如果用户想要通过键入菜单标题的开头来在菜单中导航,则您将失去键盘导航,如果您的标题需要动态,则可能无法使用键盘导航,但根据此功能的上下文,值得一试。
类似于此菜单的最后一项:
On the left side of the menu, it's easy, and even possible from Interface Builder.
For the right side, I assume you have read the Apple doc Views in Menu Items. I agree, its far more complicated than it needs to be, with many drawbacks.
Have you considered a hacky workaround?
Just create your menus with no title, and set up an icon that is a very wide icon image, including your title and icon.
You loose the keyboard navigation if the user wants to navigate in your menu by typing the beginning of the menu's title, and it might not work if your titles need to be dynamic, but depending on the context of this feature, it's worth a shot.
Similar to the last item of this menu: