如何使用NSPopover制作菜单栏应用程序

如何使用NSPopover制作菜单栏应用程序

本文介绍了如何使用NSPopover制作菜单栏应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我已经看到很多带有菜单栏项的应用程序或仅带有菜单栏界面的应用程序.

I have seen a lot of applications with a Menubar Item or applications with only a Menubar interface.

互联网上有一些教程和资料向您展示如何实现这一目标.但事实是,那些确实只有可点击的索引行.

There are some tutorials and stuff on the internet showing you how to accomplish that. But the thing is, those do only have clickable index rows in them.

当您单击菜单栏图标/项时,我希望显示一个NSPopover.有人知道怎么做吗?

I would want to have a NSPopover appear when you click the Menubar Icon / Item. Anybody who knows how to make this?

推荐答案

我不知道是否可以使用标准状态栏项目完成此操作.使用菜单的自定义视图相对容易.

I don't know if it can be done with a standard status bar item. Using a custom view for the menulet it's relatively easy.

使用自定义视图创建状态栏项:

Create a status bar item with a custom view:

item = [[NSStatusBar systemStatusBar] statusItemWithLength:thickness];
view = [[CustomView alloc] initWithFrame:(NSRect){.size={thickness, thickness}}];
[item setView:view];

您的自定义视图需要检测鼠标单击:

Your custom view needs to detect mouse clicks:

- (void)mouseDown:(NSEvent *)event {
   ...
}

最后,在检测到鼠标单击后的某个时候,显示/隐藏弹出窗口.

And finally, at some point after detecting the mouse click, show/hide the popover.

if (/* menulet is active */) {
    [popover showRelativeToRect:/* menulet view frame */
                         ofView:/* menulet view */
                  preferredEdge:NSMinYEdge];
} else {
    [popover performClose:nil];
}

您需要 NSWindow泛滥的使文本字段在弹出框内起作用.

You need a bit of NSWindow swizzling to get text fields working inside the popover.

我已经准备了一个最小的 Xcode项目,其中包含了这些想法和一些胶水: PopoverMenulet .

I've prepared a minimal Xcode project with these ideas and some glue: PopoverMenulet.

这篇关于如何使用NSPopover制作菜单栏应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:45