add customView to NSStatusItem

馋奶兔 提交于 2021-02-07 09:46:44

问题


I would like to create a status bar application which has non-menu style. same as FaceTab for facebook (I mean only interface, not functional)... this is my codes:

-(void)awakeFromNib{
    statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
    [statusItem setView:customView];
    //[statusItem setMenu:menu];
    [statusItem setTitle:@"Status"];
    [statusItem setHighlightMode:YES];
}

..... so once I Use NSMenu everything works fine but when I use NSView and CustomView outlet nothing appear on menu bar. Help plz!


回答1:


There are several moving parts involved, so the best advice I can give is to check out this excellent example project from Vadim Shpakovski.




回答2:


At the end of your awakeFromNib method, you may need to call retain on the statusItem so that it doesn't go out of scope. I was struggling with this same problem, and adding [statusItem retain]; fixed it so that I now see my Status menu in the Mac OS status bar.

-(void)awakeFromNib{
    statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
    [statusItem setView:customView];

    // in my code, this is uncommented, and menu is an instance variable.
    //[statusItem setMenu:menu];

    [statusItem setTitle:@"Status"];
    [statusItem setHighlightMode:YES];

    // this was needed to get the icon to display in the status bar.
    [statusItem retain];
}


来源:https://stackoverflow.com/questions/7770539/add-customview-to-nsstatusitem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!