简体   繁体   中英

NSStatusItem Button Title to the Right of the Image

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:

例

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 ?

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.

基于http://tinkker.io/swift/2015/11/10/nsstatusitem.html ,在Swift中看起来像button.imagePosition = NSCellImagePosition.ImageLeft做到了

采用

button.imagePosition = NSControl.ImagePosition.imageLeft

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM