Swift 状态栏中的自定义视图未在辅助屏幕上显示为禁用
我有一个应用程序,它使用macOS菜单栏中的自定义视图。macOS有一个功能,菜单栏中的项目将在辅助屏幕上显示为禁用(我认为将在视图中添加alpha值) 当我从按钮中删除自定义视图时,一切正常。但是,当我使用自定义视图时,无论它是主监视器还是辅助监视器,视图的外观总是相同的 即使设置属性“appearsDisabled”也不会更改视图的外观 这是我正在使用的代码:Swift 状态栏中的自定义视图未在辅助屏幕上显示为禁用,swift,macos,cocoa,Swift,Macos,Cocoa,我有一个应用程序,它使用macOS菜单栏中的自定义视图。macOS有一个功能,菜单栏中的项目将在辅助屏幕上显示为禁用(我认为将在视图中添加alpha值) 当我从按钮中删除自定义视图时,一切正常。但是,当我使用自定义视图时,无论它是主监视器还是辅助监视器,视图的外观总是相同的 即使设置属性“appearsDisabled”也不会更改视图的外观 这是我正在使用的代码: private let statusItem = NSStatusBar.system.statusItem(withLength:
private let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
private var view: HostView?
func applicationDidFinishLaunching(_ aNotification: Notification)
{
self.createMainView()
self.createMenuBarView()
}
fileprivate func createMenuBarView()
{
// Remove all sub views from the view and create new ones.
self.view?.subviews.removeAll()
var width: CGFloat = 0
for device in self.controller.model.devices
{
if let newView = self.createView(for: device.value, x: width)
{
self.view?.addSubview(newView.view)
width += newView.width
}
}
self.view?.frame = NSRect(x: 0, y: 0, width: width, height: MenuBar.height)
self.statusItem.image = nil
self.statusItem.length = width
if let view = self.view
{
// Do I have to set some properties here?
self.statusItem.button?.addSubview(view)
}
}
fileprivate func createMainView()
{
let view = HostView(frame: NSRect(x: 0, y: 0, width: 32.0, height: MenuBar.height))
view.menu = self.menu
self.view = view
}
问题似乎是我正在将NSView作为子视图添加到NSStatusItem的按钮中
self.statusBarItem.button?.addSubview(myView)
当我将我的自定义视图设置为NSStatusItem的view属性时,该视图在辅助屏幕上变灰(请注意,自macOS 10.14以来,该视图已被弃用)。对于具有不同子视图的自定义视图,如NSProgressingIndicator(圆形)和NSButton with image,它不适用于我。即使NSTextfield在非活动显示上也不会显示为灰色。你是如何做到这一点的?我是不是遗漏了什么?顺便说一句,它确实适用于NSImageView或使用带有文本的按钮而不是NSTextfield。