4

I want an NSStatusItem’s button’s title to appear to the right of its image. Here’s an example, this is the OS X VPN menubar app:

Example

If I set either the title or image for my NSStatusItem’s button, they render correctly. However if I set both, they get superimposed and are horizontally center-aligned.

Is there a way to achieve the side-by-side placement without creating a custom UIView?

magiclantern
  • 768
  • 5
  • 19

3 Answers3

12

This has to do with a change in Yosemite, which deprecated setTitle: and setImage: on NSStatusItem. The documentation isn't super-helpful for what to do instead, however.

I’ve found that you have to set the title and the image on the button property of the NSStatusItem. That’s how I was getting the behaviour described above, with the title and the image centred over each other.

The solution is simple but not immediately apparent. NSStatusBarItem responds to the setImagePosition: method of NSButton. Here’s the code that fixed my issue:

self.statusItem.button.image = [NSImage imageNamed:@"StatusBarIcon"];
self.statusItem.button.imagePosition = NSImageLeft;

Having done this, setting self.statusItem.button.title positions the items correctly.

magiclantern
  • 768
  • 5
  • 19
  • 1
    do you know if this has changed recently or in swift? I have this same issue, but when I've tried `self.statusItem.button.imagePosition = NSImageLeft` I get 'Use of unresolved identifier 'NSImageLeft'' – Pirijan Apr 05 '16 at 00:11
  • 1
    I also tried the values listed for `NSCellImagePosition` but no dice there either https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSCell_Class/index.html#//apple_ref/c/tdef/NSCellImagePosition – Pirijan Apr 05 '16 at 00:15
  • figured it out below – Pirijan Apr 05 '16 at 00:18
3

based on http://tinkker.io/swift/2015/11/10/nsstatusitem.html, in Swift it looks like button.imagePosition = NSCellImagePosition.ImageLeft does it

Pirijan
  • 3,430
  • 3
  • 19
  • 29
3

use

button.imagePosition = NSControl.ImagePosition.imageLeft
eos1d3
  • 255
  • 2
  • 10