MacOS 開發 — NSStatusBar + NSPopover
源碼地址:https://github.com/JHiroGuo/NSStatusBarItem
NSStatusBar : macOS系統的頂部導航欄item
@interface AppDelegate (){
NSStatusItem * statusItem;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
/* 初始化 */
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
/* 設置NSImage * /
[statusItem.button setImage:[NSImage imageNamed:@"math"]];
/* 設置點擊響應事件 */
statusItem.action = @selector(touchStatusItem:);
}
NSPopover : pop視圖
@interface AppDelegate (){
NSPopover * popover;
}
-(void)touchStatusItem:(NSStatusBarButton *)button{
/* 初始化 */
popover = [[NSPopover alloc]init];
/* 設置動畫 */
popover.behavior = NSPopoverBehaviorTransient;
/* 設置外觀 */
popover.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantLight];
/* 設置展示視圖 */
popover.contentViewController = [[PopViewController alloc]initWithNibName:@"PopViewController" bundle:nil];
/* 設置展示方位 */
[popover showRelativeToRect:button.bounds ofView:button preferredEdge:NSRectEdgeMaxY];
}
效果: