如何动态地将NSView与NSStatusBar相关联?

时间:2015-11-23 12:03:25

标签: objective-c macos nsview nsstatusitem nsstatusbar

我已经像这样创建了一个NSStatusBar NSMenu:

- (NSMenu *)startUpViewBarMenu {
    NSMenu *menu = [[NSMenu alloc] init];

    NSMenuItem* info = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
    //[info setTarget:self];
    [info setView:[self startUpView]];
    [menu addItem:info];

    // Disable auto enable
    [menu setAutoenablesItems:NO];
    [menu setDelegate:(id)self];
    return menu;
}

我想动态移动指向图标所在位置的NSView([self startUpView])。与Evernote的做法类似。如您所见,它是图标的核心:enter image description here

而对于我的NSStatusBar,NSView会落在NSStatusBar图标的左侧或右侧。

所以有两个问题:

如何移动下拉NSView?

我尝试过更改框架(-100),但没有区别:

NSView *view = [[NSView alloc] initWithFrame:NSMakeRect(-100, 0, 400, 471)];

我如何动态移动与图标相关的下拉NSView?

1 个答案:

答案 0 :(得分:1)

Evernote 使用菜单。这似乎是在点击您的statusitem视图时触发的NSPopover。

/* setup your status item */

self.statusItem.highlightMode = YES;
[self.statusItem setAction:@selector(showPopover:)];
[self.statusItem setTarget:put correct target here];


/* use this code to show popover */


 -(void)showPopover:(id)sender
 {
     NSWindow * aWindow = [sender window];
     NSView * aView = [aWindow contentView];
     NSPopover  * aPopover = [[NSPopover alloc] init];
     /* Setup your popover here */
     [aPopover showRelativeToRect:aView.bounds ofView:aView preferredEdge:NSMaxYEdge];
 }