【问题标题】:OSX Status Bar Image Sizing - CocoaOSX 状态栏图像大小调整 - Cocoa
【发布时间】:2015-11-14 00:51:03
【问题描述】:

所以我对NSStatusBar 项目的图像有问题,看起来图像正在推离其余菜单项as you can see in this picture. 但是当菜单栏处于非活动状态时(如我在我的其他显示器上或不在应用程序中)问题不会发生as you can see in this picture。不过我很确定我的代码是正确的。

statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setHighlightMode:YES];
[statusItem setAction:@selector(openWindow)];
[statusItem setTarget:self];

if ([[[NSAppearance currentAppearance] name] containsString:NSAppearanceNameVibrantDark]) {
    [statusItem setImage:[NSImage imageNamed:@"whiteMenu.png"]];
} else {
    [statusItem setImage:[NSImage imageNamed:@"blackMenu.png"]];
}

我查看了这个问题:Display image in a cocoa status app 但问题仍然存在,所以我不知道还能做什么,感谢您的帮助! PS:我认为是NSVariableStatusItemLength的问题,我试过NSSquareStatusItemLength但没有运气,也尝试自己设置,但同样的问题,但几乎没有改善。

【问题讨论】:

    标签: objective-c macos cocoa nsstatusitem nsstatusbar


    【解决方案1】:

    在实现显示在菜单栏中的漂亮NSStatusItem 时,我也遇到了一些问题。
    当项目(和随附的资产)满足以下条件时,我得到了最好的结果

    • 状态图像应基于 PDF
    • ...并使用“模板”后缀(更多关于here
    • 满足上述条件将为您提供 HiDPI 支持以及明暗菜单栏的正确渲染
    • 图片大小应设置为(18.0, 18.0)
    • 要在状态栏中获得与默认 OS X 项目相同的突出显示,highlightMode 必须打开,并且该项目必须具有关联的 NSMenu

    这个 sn-p 将为您提供一个基本应用程序,在系统主菜单中带有漂亮的 NSStatusItem(我在这里使用了系统提供的图像,但您可以使用带有“模板”的自定义 18x18 PDF 获得相同的结果" 名称后缀):

    @interface AppDelegate ()
    
    @property NSStatusItem* statusItem;
    @property (weak) IBOutlet NSMenu *statusMenu;
    
    @end
    
    @implementation AppDelegate
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        NSImage* statusImage = [NSImage imageNamed:NSImageNameActionTemplate];
        statusImage.size = NSMakeSize(18.0, 18.0);
        self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
        self.statusItem.image = statusImage;
        self.statusItem.highlightMode = YES;
        self.statusItem.enabled = YES;
        self.statusItem.menu = self.statusMenu;
    }
    
    - (void)applicationWillTerminate:(NSNotification *)aNotification {
        // Insert code here to tear down your application
    }
    
    @end
    

    【讨论】:

    • 你的答案要好得多,但事实证明我的问题的另一个答案只是摆脱控制状态栏的计时器,因为我有一个计时器来检查是否菜单栏外观已更改,但无论如何感谢!不知道我必须使用 pdf!
    猜你喜欢
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 2010-11-10
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多