OSX状态栏图像大小 - Cocoa

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

我看到了这个问题:在cocoa状态应用程序中显示图像,但问题仍然存在,所以我不确定还能做什么,感谢任何帮助!PS:我认为问题出在NSVariableStatusItemLength上,我尝试了NSSquareStatusItemLength,但没有成功,也尝试了手动设置它,但是问题依旧存在,只有一点改进。
1个回答

13

在实现一个外观漂亮的在菜单栏中显示的 NSStatusItem 时,我也遇到了一些问题。以下是项目和相关资源应满足的标准:

  • 状态图像应该基于 PDF
  • ......并使用“Template”后缀(有关详细信息,请参见此处
  • 满足以上要求将为您提供HiDPI支持,并且轻色和暗色菜单栏均能正确呈现
  • 图像大小应设置为(18.0,18.0)
  • 为了获得与默认 OS X 状态栏中的项目相同的突出显示效果,highlightMode 必须开启,并且该项目必须具有关联的 NSMenu

下面的代码段将为您提供一个基本的应用程序和一个外观漂亮的 NSStatusItem ,该项位于系统主菜单中(我在这里使用了系统提供的图像,但您也可以使用一个定制的18x18 PDF并使用“Template”名称后缀来获得相同的结果):

@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

1
你的答案更好,但事实证明我的问题另一个答案是摆脱控制状态栏的定时器,因为我将其设置为定时器以检查菜单栏外观是否已更改,但还是谢谢!我不知道我必须使用PDF! - Bolt Sandwich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接