我有一个应用程序,它使用macOS菜单栏中的自定义视图 . macOS具有以下功能:菜单栏中的项目将在辅助屏幕上显示为禁用(我认为alpha值将添加到视图中) .

当我从Button中删除自定义视图时,一切正常 . 但是当我使用自定义视图时,无论是主监视器还是辅助监视器,视图总是看起来都一样 .

即使设置属性“appearDisabled”也不会更改视图的外观 .

这是我正在使用的代码:

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
}