views:

115

answers:

1

I have created a status item (menu bar item) for my app, but at the moment it's title is text, how would I make it show an image?

This is the part of the code which sets the title. What would I need to change to that code to make it display an image?

[item setTitle:@"MenuItem"];
+7  A: 

You use the setImage: method, supplying it with an NSImage which you want to display on the menu bar.

Note also that you can have an NSStatusItem showing an image and text, for example with the battery indicator.

Here is a small example setting the image to a file named OtherImage.jpg:

NSImage *statusItemImage = [NSImage imageNamed:@"OtherImage"];
if( !statusItemImage ) {
  NSLog(@"ERROR: Could not load the image for 'OtherImage.png'");
} else {
  [item setImage:statusItemImage];
}

Make sure that the file OtherImage.png is actually added to your Xcode project and copied into the application's Resources directory. If your image does not load, it means that the file is not named correctly, not really an image, or not actually in the application bundle.

Perspx
Ah I see, so i would replace setTitle:@"MenuItem" with setImage:(NSImage *) , but where do i enter the location of my image?
Joshua
You load the image first. You can use something like NSImage *statImage = [NSImage imageNamed:@"MyStatusImage"]; Just make sure that MyStatusImage.png or whatever is actually in your application's resources folder.
Jason Coco
Where would I put the code NSImage *statImage = [NSImage imageNamed:@"MyStatusImage"]; ?
Joshua
How about this code setImage:[[NSBundle mainBundle] pathForResource:@"OtherImage" ofType:@"png"]]; ?
Joshua
Joshua: setImage: takes an image. pathForResource:ofType: returns a string. String != image. You need to load the image that is at that path; the NSImage class has methods to do that.
Peter Hosey
So what code should I replace [item setTitle:@"MenuItem"]; with?
Joshua
Google for NSImage, the first hit is the class documentation. In there you can find how to initialize an image from a file path with approximately 5 minutes of reading work.
Dirk Stoop
@Joshua - I added some code to Perspx's answer, but you really should check out the docs and do some google searches. It seems you may want to read up on some of the Cocoa basics, like the architecture document and the MVC design guidelines. Cheers!
Jason Coco
Yes, that works! Thanks, one thing though how would I set the highlighted image because at the moment I've just got this code [item setHighlightMode:YES]; and it only works with the text.
Joshua
@Jason - Cheers for adding the code sample
Perspx
@Joshua - Use setAlternateImage: on item. Please take a look at the documentation. Perspx pervided a link to the document for the NSStatusItem class just there.
Jason Coco
I used the code to set the Status item image and changed it to setAlternateImage: but now it won't let you click the Status Item.
Joshua
I used the code to set the Status item image and changed it to setAlternateImage: but now it won't let you click the Status Item. How come?
Joshua
I used the code to set the Status item image and changed it to setAlternateImage: but now it won't let you click the Status Item. How come? Looking for an answer please.
Joshua