单击 NSStatusItem 时,我会弹出一个自定义窗口。该代码基于MAAtachedwindow。一切都很好,但是当用户单击其他状态栏项目或其他应用程序等其他内容时,我无法找到关闭窗口的方法。
这是我创建窗口的代码:
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:width] retain];
//setup custom status menu view
CGFloat height = [[NSStatusBar systemStatusBar] thickness];
NSRect viewFrame = NSMakeRect(0.0f, 0.0f, width, height);
statusMenuView = [[[_ISStatusMenuView alloc] initWithFrame:viewFrame] retain];
statusMenuView.offset = aOffset;
statusItem.view = statusMenuView;
//setup the window to show when clicked
NSRect contentRect = NSZeroRect;
contentRect.size = aView.frame.size;
statusMenuWindow = [[[NSWindow alloc] initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO] retain];
[statusMenuWindow setLevel:NSPopUpMenuWindowLevel];
[statusMenuWindow setBackgroundColor:[NSColor clearColor]];
[statusMenuWindow setMovableByWindowBackground:NO];
[statusMenuWindow setExcludedFromWindowsMenu:YES];
[statusMenuWindow setOpaque:NO];
[statusMenuWindow setHasShadow:NO];
[statusMenuWindow useOptimizedDrawing:YES];
[[statusMenuWindow contentView] addSubview:aView];
[statusMenuWindow setDelegate:self];
statusMenuView.statusMenuWindow = statusMenuWindow;
这是我显示窗口的方式:
- (void)centerView{
NSRect menuFrame = self.window.frame;
NSRect windowFrame = self.statusMenuWindow.frame;
NSPoint menuPoint = NSMakePoint(NSMidX(menuFrame), NSMinY(menuFrame));
menuPoint.x -= windowFrame.size.width*0.5f;
menuPoint.y -= windowFrame.size.height+self.offset;
[self.statusMenuWindow setFrameOrigin:menuPoint];
[self.statusMenuWindow makeKeyAndOrderFront:self];
}
我希望windowDidResignKey
委托方法可以解决问题,但这种配置并不能解决问题。代表正在工作,因为windowDidMove
确实在运行。
- (void)windowDidResignKey:(NSNotification *)notification{
NSLog(@"windowDidResignKey");
[statusMenuView hideView];
}
- (void)windowDidResignMain:(NSNotification *)notification{
NSLog(@"windowDidResignMain");
}
- (void)windowDidMove:(NSNotification *)notification{
NSLog(@"windowDidMove");
}
所以回顾一下,当用户点击其他任何东西时,我如何隐藏我的自定义窗口,标准状态栏菜单的工作方式?
编辑 查看Popup示例后,我唯一缺少的是我必须继承 NSPanel 并使其成为关键窗口。
@interface Panel : NSPanel
@end
@implementation Panel
- (BOOL)canBecomeKeyWindow{
return YES;
}
@end