xcode 如何使用 NSPopover 制作菜单栏应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7837439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a Menubar Application with a NSPopover?
提问by Jacob
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.
互联网上有一些教程和内容向您展示了如何做到这一点。但问题是,那些只有可点击的索引行。
I would want to have a NSPopover appear when you click the Menubar Icon / Item. Anybody who knows how to make this?
当您单击菜单栏图标/项目时,我希望出现 NSPopover。有谁知道这个怎么做?
回答by djromero
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.
我不知道是否可以使用标准状态栏项来完成。为 menulet 使用自定义视图相对容易。
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];
}
You need a bit of NSWindow swizzlingto get text fields working inside the popover.
你需要一些 NSWindow swizzling来让文本字段在弹出框内工作。
I've prepared a minimal Xcode projectwith these ideas and some glue: PopoverMenulet.
我已经用这些想法和一些胶水准备了一个最小的Xcode 项目:PopoverMenulet。