5

因此,我对某个项目的图像有疑问,就像您在这张图片中看到的NSStatusBar那样,该图像似乎正在远离其余的菜单项。但是,当菜单栏处于非活动状态时(如我在我的另一台显示器上或不在应用程序中),问题不会发生,正如您在这张图片中看到的那样。我很确定我的代码是正确的。

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

我查看了这个问题:在可可状态应用程序中显示图像但问题仍然存在,所以我不确定还能做什么,感谢您的帮助!PS:我认为的问题是NSVariableStatusItemLength,我试过NSSquareStatusItemLength但没有运气,也尝试过自己设置,但同样的问题,但几乎没有改善。

4

1 回答 1

12

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

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

此代码段将为您提供一个 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
于 2015-11-14T12:30:51.557 回答