【问题标题】:How to dynamically place NSView in relation to NSStatusBar?如何动态地将 NSView 与 NSStatusBar 相关联?
【发布时间】:2015-11-23 12:03:25
【问题描述】:

我已经像这样创建了一个 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 的做法。如您所见,它位于图标的中心:

而对于我的 NSStatusBar,NSView 位于 NSStatusBar 图标的左侧或右侧。

所以两个问题:

如何移动下拉菜单 NSView?

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

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

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

【问题讨论】:

    标签: objective-c macos nsview nsstatusitem nsstatusbar


    【解决方案1】:

    Evernote使用菜单。这似乎是在单击您的状态项视图时触发的 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];
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 2010-12-28
      • 1970-01-01
      相关资源
      最近更新 更多