gpt4 book ai didi

swift - 状态栏中的自定义 View 未在辅助屏幕上显示为禁用

转载 作者:行者123 更新时间:2023-11-30 10:58:16 27 4
gpt4 key购买 nike

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

当我从按钮中删除自定义 View 时,一切正常。但是当我使用自定义 View 时,无论是主显示器还是辅助显示器, View 总是看起来相同。

即使设置属性“appearsDisabled”也不会改变 View 的外观。

这是我正在使用的代码:

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 的按钮作为 subview 。

self.statusBarItem.button?.addSubview(myView)

当我将自定义 View 设置为 NSStatusItem 的 view-Property 时,该 View 在辅助屏幕上呈灰色(请注意,自 macOS 10.14 起已弃用)。

关于swift - 状态栏中的自定义 View 未在辅助屏幕上显示为禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53699650/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com