Date
Dec. 22nd, 2024
 
2024年 11月 22日

Post: OS X: Create Status Bar Item

OS X: Create Status Bar Item

Published 12:04 Apr 01, 2015.

Created by @ezra. Categorized in #Programming, and tagged as #macOS.

Source format: Markdown

Table of Content

直入主题, 为 OS X APP 添加菜单栏图标:

NSStatusItem *statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
statusItem.image = [NSImage imageNamed:@"normal"];
statusItem.alternateImage = [NSImage imageNamed:@"highlight"];
statusItem.action = @selector(clickStatusItem);

如果你使用菜单, 也可以设置:

NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Menu"];
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"Demo" action:nil keyEquivalent:@""];
NSMenuItem *quit = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(quit) keyEquivalent:@""];
[menu addItem:item];
[menu addItem:[NSMenuItem separatorItem]];
[menu addItem:quit];
statusItem.menu = menu;

针对 NSMenu 你可以进一步自定义, 比如快捷键等。

Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
开发、设计与写作皆为所长。
热爱苹果、钟情色彩。
随时恭候 垂询