如何在菜单栏中获得类似聚光灯的文本输入效果?
我想在我的 Mac 应用程序的菜单栏中有一个图标 - 并且该图标应在单击时生成一个菜单。虽然菜单中有更多条目,但我希望将顶行作为通用文本输入字段 - 就像在 Spotlight 中一样。
是否可以将这样的字段添加到 NSMenu
中?或者我应该把它做成面板型窗口?
I want to have an icon in the menubar in my Mac app - and the icon should spawn a menu upon clicking. While having more entries in the menu, I would like to have a top row as a universal text entry field - like it is in Spotlight.
Is it possible to add such a field to NSMenu
? Or should I do it as a panel-type window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用的是 xcode 4 ,请在界面生成器中创建自定义视图并添加文本字段或任何您想要的内容。在 IB 中,还可以从对象库中拖放一个“菜单”,其中包含任意数量的项目。然后,只需按住 Ctrl 键并单击要放入文本字段的菜单项(在您的情况下,它将是顶部的菜单项),然后拖动到自定义视图并选择“视图”。现在,当您打开菜单时,它不会显示该空间中的菜单项,而是显示自定义视图中的所有内容。
编辑:至于你的评论,这是你应该做的。通过打开助理编辑器视图并按住 Ctrl 键并从菜单中单击到要使用的头文件,使菜单成为一个出口。现在,只需创建一个每当菜单打开时就会运行的方法,苹果已经做了这个,它被称为 menuWillOpen。
0 处的延迟将使其立即发生,它必须在通用模式运行循环中完成,以便菜单即使在打开时也会更新。现在只需创建
methodExecutedWhenMenuIsClicked
并设置它以便文本字段做出响应。If you're using xcode 4 , make a custom view in interface builder and add a textfield or anything you want to it. In IB also drag and drop a "Menu" from the objects library with as many items as you want in it. Then simply ctrl+click the menu item you want to make into the text field (In your case it would be the top one) and drag to the custom view and select "view". Now when you open the menu, instead of showing a menu item in that space, it shows whatever was in your custom view.
EDIT: As for your comment here's what you should do. Make your menu an outlet by opening the assistant editor view and ctrl+click from your menu to the header file that you want to use. now, simply make a method that will run whenever the menu will open, conveniently apple already made this, it's called menuWillOpen.
the delay at 0 will make it happen immediately, it must be done in the common modes run loop so that the menu will be updated even while it's open. Now just make the
methodExecutedWhenMenuIsClicked
and set it so the text field responds.您可以使用
-[NSMenuItem setView:]
将任何视图放入菜单中。请参阅 NSMenuItem.h 中的长注释以及应用程序菜单和弹出列表编程主题中的菜单中的视图部分。You can put any view in a menu using
-[NSMenuItem setView:]
. See the long comment in NSMenuItem.h and the section Views in Menus in Application Menu and Pop-up List Programming Topics.你可能会很挣扎。我只是尝试做同样的事情,并阅读 Ahruman 引用的应用程序菜单和弹出列表编程主题文档中的菜单中的视图,我发现了这一点:
我想我们是SOL。显然,Spotlight 会弹出一个无边框窗口。
You're probably going to struggle quite a bit. I just tried doing the same thing, and reading the Views in Menus in Application Menu and Pop-up List Programming Topics document referenced by Ahruman, I found this:
I think we're SOL. Apparently Spotlight pops up a borderless window instead.