简体   繁体   English

图像右侧的NSStatusItem按钮标题

[英]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. 我希望NSStatusItem的按钮标题显示在其图像的右侧。 Here's an example, this is the OS X VPN menubar app: 这是一个示例,这是OS X VPN菜单栏应用程序:

例

If I set either the title or image for my NSStatusItem 's button, they render correctly. 如果我为NSStatusItem的按钮设置标题或图像,它们将正确呈现。 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 ? 有没有一种方法可以在不创建自定义UIView情况下实现并排放置?

This has to do with a change in Yosemite, which deprecated setTitle: and setImage: on NSStatusItem . 这与Yosemite中的更改有关,后者已弃用NSStatusItem上的setTitle:setImage: 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 . 我发现您必须在NSStatusItembutton属性上设置标题和图像。 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 . NSStatusBarItem响应NSButtonsetImagePosition:方法。 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. 完成此操作后,设置self.statusItem.button.title正确self.statusItem.button.title项目。

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

采用

button.imagePosition = NSControl.ImagePosition.imageLeft

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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