Objective c OSX状态栏图像大小-Cocoa

Objective c OSX状态栏图像大小-Cocoa,objective-c,macos,cocoa,nsstatusitem,nsstatusbar,Objective C,Macos,Cocoa,Nsstatusitem,Nsstatusbar,因此,我对NSStatusBar项的图像有问题,似乎图像正在远离其余菜单项,但当菜单栏处于非活动状态时(如我在其他显示器上或不在应用程序中),问题不会发生。不过我很确定我的代码是正确的 statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; [statusItem setHighlightMode:YES]; [statusItem setAction:@selec

因此,我对
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
,但运气不好,我也尝试了自己设置,但遇到了同样的问题,但是没有什么改进。

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

  • 状态图像应基于PDF
  • 。。。并使用“模板”后缀(更多信息)
  • 满足上述条件将为您提供HiDPI支持,并为明暗菜单栏提供适当的渲染
  • 图像的大小应设置为(18.0,18.0)
  • 要在状态栏中获得与默认OS X项目相同的高亮显示,必须启用highlightMode,并且该项目必须具有关联的NSMenu
这段代码将为您提供一个基本应用程序,在系统主菜单中有一个漂亮的
NSStatusItem
(我在这里使用了系统提供的图像,但您可以使用带有“模板”名称后缀的自定义18x18 PDF获得相同的结果):


你的答案要好得多,但事实证明,我的问题的另一个答案只是为了摆脱控制状态栏的计时器,因为我用计时器检查菜单栏的外观是否发生了变化,不过还是要谢谢你!我一点也不知道我必须使用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