Search code examples
objective-cmacosnsviewnsstatusitemnsstatusbar

How to dynamically place NSView in relation to NSStatusBar?


I have created a NSStatusBar NSMenu like this:

- (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;
}

I would like to dynamically move the NSView ([self startUpView]) that points to where the icon is. Similar to how Evernote has done it. As you can see it is central to the icon: enter image description here

Whereas with my NSStatusBar the NSView falls either to the left or right of the NSStatusBar icon.

So two questions:

How can I move the dropdown NSView?

I have tried changing the frame (-100) but it makes no difference:

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

How can I dynamically move the dropdown NSView in relation to the icon?


Solution

  • Evernote does not use a menu. This seems to be a NSPopover that is triggered when clicking on your statusitem's view.

    /* 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];
     }