MacOS 開發 — NSStatusBar + NSPopover

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

效果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章