如何添加图标到NSMenuItem的右侧 [英] How to add icons to the right hand side of NSMenuItem

查看:557
本文介绍了如何添加图标到NSMenuItem的右侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在菜单项右侧添加一些图标,最好是右对齐。我知道NSMenuItem允许您添加菜单项(例如,请参见 MenuMadness 示例)。我真的在寻找像OS X Wifi菜单中的安全/信号强度指示器。

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.

我看到的一种方法是将项目添加到菜单,并使用setView为每个项目提供一个NSView。然而,这似乎比它应该是更复杂。是否有一种方法只是添加一个图标/多个图标到菜单?

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?

推荐答案

在菜单左侧简单,甚至可以从Interface Builder。

On the left side of the menu, it's easy, and even possible from Interface Builder.

右侧,我假设你已经阅读Apple doc 菜单项中的视图。我同意,它比它需要的复杂得多,有很多缺点。

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:

这篇关于如何添加图标到NSMenuItem的右侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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