[英]How to draw custom view in NSStatusBar?
我目前有一個NSStatusBar
和一個帶有標題顯示的NSStatusBarItem
。 但我不想用自定義NSView
替換標題/文本。 我怎樣才能做到這一點?
我目前有:
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
我怎樣才能做到這一點? 我試圖將它添加為按鈕的子視圖,但沒有運氣。
創建對類頂層狀態項的強引用
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) }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.