如何在 NSStatusBar 中绘制自定义视图?
How to draw custom view in NSStatusBar?
我目前有一个 NSStatusBar
和一个显示标题的 NSStatusBarItem
。但我不想用自定义 NSView
替换 title/text。我该怎么做?
我目前有:
func applicationDidFinishLaunching(_ aNotification: Notification) {
let statusBar = NSStatusBar.system
statusBarItem = statusBar.statusItem(withLength: NSStatusItem.variableLength)
statusBarItem.button?.title = "My Menu Bar App"
}
假设我不想用以下内容替换标题:
let view = NSView(frame: NSRect(x: 0, y: 0, width: 40, height: 40))
view.layer?.backgroundColor = NSColor.yellow.cgColor
我怎样才能做到这一点?我试图将它作为子视图添加到按钮,但没有成功。
创建对 class
顶层状态项的强引用
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
在applicationDidFinishLaunching
中启用图层并将视图添加到按钮的子视图中
func applicationDidFinishLaunching(_ aNotification: Notification) {
let view = NSView(frame: NSRect(x: 0, y: 0, width: 22, height: 22))
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.yellow.cgColor
statusItem.button?.addSubview(view)
}
我目前有一个 NSStatusBar
和一个显示标题的 NSStatusBarItem
。但我不想用自定义 NSView
替换 title/text。我该怎么做?
我目前有:
func applicationDidFinishLaunching(_ aNotification: Notification) {
let statusBar = NSStatusBar.system
statusBarItem = statusBar.statusItem(withLength: NSStatusItem.variableLength)
statusBarItem.button?.title = "My Menu Bar App"
}
假设我不想用以下内容替换标题:
let view = NSView(frame: NSRect(x: 0, y: 0, width: 40, height: 40))
view.layer?.backgroundColor = NSColor.yellow.cgColor
我怎样才能做到这一点?我试图将它作为子视图添加到按钮,但没有成功。
创建对 class
顶层状态项的强引用let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
在
applicationDidFinishLaunching
中启用图层并将视图添加到按钮的子视图中func applicationDidFinishLaunching(_ aNotification: Notification) { let view = NSView(frame: NSRect(x: 0, y: 0, width: 22, height: 22)) view.wantsLayer = true view.layer?.backgroundColor = NSColor.yellow.cgColor statusItem.button?.addSubview(view) }