Skip to content

Instantly share code, notes, and snippets.

@maxsteenbergen
Created May 13, 2013 09:18
Show Gist options
  • Save maxsteenbergen/5567139 to your computer and use it in GitHub Desktop.
Save maxsteenbergen/5567139 to your computer and use it in GitHub Desktop.
This should draw an icon in the Status Bar. It works on my MacBook, but on my iMac it doesn't show an icon. The menulet is there, I can click it: it simply doesn't show the icon.
- (void)drawRect:(NSRect)rect
{
/* NSImage *img = [NSImage imageNamed:@"basket@2x.png"];
rect = CGRectInset(rect, 2, 3);
NSRect imageRect;
imageRect.origin = NSZeroPoint;
imageRect.size = [img size];
[img drawInRect:rect fromRect:imageRect operation:NSCompositeSourceOver fraction:1];
*/
NSImage *img = [NSImage imageNamed:@"basket.png"];
NSStatusItem *statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setImage:img];
[statusItem setHighlightMode:YES];
[statusItem setEnabled:YES];
}
@alloy
Copy link

alloy commented May 13, 2013

I’m not sure that this will fix the difference, but basically drawRect: is not the place to setup your views. Instead it is the place where a view does it's actual drawing. I suggest you move the uncommented code into an init method and remove the drawRect: method, unless you really need to perform custom drawing code.

@tarnfeld
Copy link

I may be wrong, but I seem to remember that you need too keep a reference to the statusItem for it to stay visible... Perhaps stick it into an ivar?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment