在Cocoa状态应用中显示图像

9

你好,我开发了一个可可状态应用程序。 当我输入一个长标题时,例如,它无法显示,并且如果我将图像作为图标也无法显示,但是如果我输入一个小标题,则可以正常工作。 如何解决这个问题并显示图像?

statusItem = [[[NSStatusBar systemStatusBar] 
    statusItemWithLength:NSSquareStatusItemLength] retain];    

[statusItem setMenu:menu]; 
//[statusItem setTitle:@"Notif "]; 
[statusItem setImage:[NSImage imageNamed:@"image"]]; 
[statusItem setHighlightMode:YES];

有什么建议吗? - user1503496
哦,你想在菜单或状态栏中显示图像? - Matthieu Riegler
1个回答

9
基本上,
NSStatusItem *statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
NSImage* icon = [NSImage alloc] initWith ...]
[statusItem setImage:icon];

但是你的图像必须是正确的尺寸(小于20*20)。

statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain]; [statusItem setMenu:menu]; //[statusItem setTitle:@"Notif "]; [statusItem setImage:[NSImage imageNamed:@"image"]]; [statusItem setHighlightMode:YES];这是我的代码,我已经将图像添加到其他资源文件夹中。 - user1503496
我怎样能够使尺寸小于20×20? - user1503496
1
[icon setSize:NSMakeSize(width,height)]。 确保 [NSImage imageNamed:@"image"] 不会返回 nil。 - Matthieu Riegler
2
非Retina屏幕的图像最大尺寸为21x21,而不是20x20。对于Retina屏幕,最大尺寸为41x41;请参见我的博客文章获取更多信息。 - al45tair

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