macOS创建系统顶部栏Icon图标及交互

2023-07-16  本文已影响0人  Living_元宝儿

创建状态栏图标


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    //获取系统单例NSStatusBar对象

        self.statusBar= [NSStatusBarsystemStatusBar];

        //创建固定宽度的NSStatusItem

        _statusItem = [self.statusBar statusItemWithLength:NSVariableStatusItemLength];

        [_statusItem.buttonsetTarget:self];

        [_statusItem.buttonsetAction:@selector(itemAction:)];

        _statusItem.button.image = [NSImage imageNamed:@"toplogoIcon"];

}

// 自定义view设置弹出框内容
- (void)itemAction:(NSButton *)button{
    NSPopover *popover = [[NSPopover alloc] init];
    popover.behavior = NSPopoverBehaviorApplicationDefined;
    popover.contentViewController = [NSViewController new];
    popover.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantLight];
    [popover showRelativeToRect:button.bounds ofView:button preferredEdge:NSRectEdgeMaxY];
}
上一篇下一篇

猜你喜欢

热点阅读