我正在为OSX开发一个状态栏应用,并且我试图复制Dropbox使用的popover的行为,到目前为止,我设法让所有想要的行为都正常工作除了当我打开任务控制时,弹出窗口没有消失,它只是保持在顶部覆盖任务控制的一部分。我在过去几天里搜索得很辛苦,还没有找到解决方案。我能想到的唯一一件事就是让听众听一个向上的3个手指轻扫?获得像popover一样的Dropbox行为一直是一个皇家的痛苦,所以我计划将一个示例应用程序上传到GitHub,一旦我得到这一切,以帮助节省人们一些时间试图找到变通方法!有没有人遇到过这个问题?干杯!
这是具有popover" MenuBarController"的逻辑的类。 #import" MenuBarController.h"
@implementation MenuBarController
@synthesize statusItem;
@synthesize popover;
@synthesize popoverTransiencyMonitor;
-(id)init{
self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
self.popover = [NSPopover new];
popover.behavior = NSPopoverBehaviorTransient;
return self;
}
-(void)runMenuBarItem{
// The text that will be shown in the menu bar
statusItem.title = @"";
// The image that will be shown in the menu bar, a 16x16 black png works best
statusItem.image = [NSImage imageNamed:@"redicon"];
[statusItem setEnabled:YES];
[statusItem setHighlightMode:YES];
[statusItem setTarget:self];
[statusItem setAction:@selector(togglePopover:)];
}
- (void) showPopover:(id)sender {
MenuBarPopOverViewController * viewController = [[MenuBarPopOverViewController alloc] initWithNibName:@"MenuBarPopOverViewController" bundle:nil];
popover.contentViewController = viewController;
[popover showRelativeToRect:NSZeroRect ofView:(NSView *)sender preferredEdge:NSMinYEdge];
}
- (void) closePopover:(id)sender {
[popover performClose:sender];
}
-(void)togglePopover:(id)sender{
if(popover.shown){
[self closePopover:sender];
}else{
[self showPopover:sender];
}
if (self.popoverTransiencyMonitor == nil) {
self.popoverTransiencyMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSLeftMouseDownMask | NSRightMouseDownMask | NSKeyUpMask) handler:^(NSEvent* event) {
[NSEvent removeMonitor:self.popoverTransiencyMonitor];
self.popoverTransiencyMonitor = nil;
[self.popover close];
}];
}
}
@end