如何使用 NSPopover 制作菜单栏应用程序? [英] How do I make a Menubar Application with a NSPopover?

查看:18
本文介绍了如何使用 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 swizzling获取在弹出框内工作的文本字段.

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 制作菜单栏应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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