swiftmacoscocoansstatusitemnsstatusbar

How to draw custom view in NSStatusBar?


I currently have a NSStatusBar with a NSStatusBarItem with a title displaying. But I wan't to replace the title/text with a custom NSView. How can I do that?

I currently have:

func applicationDidFinishLaunching(_ aNotification: Notification) {
    let statusBar = NSStatusBar.system
    statusBarItem = statusBar.statusItem(withLength: NSStatusItem.variableLength)
    statusBarItem.button?.title = "My Menu Bar App"
}

Let's say I wan't to replace the title with the following:

let view = NSView(frame: NSRect(x: 0, y: 0, width: 40, height: 40))
view.layer?.backgroundColor = NSColor.yellow.cgColor

How can I achieve that? I tried to add it as a subview to the button, with no luck.


Solution