Reputation: 43
I wand to build a two line menu item in a taskbar Swift Application for MacOS, that behaves like a normal menu item, just with additional information. I managed to get the menu and the taskbar application going, but failed with the custom view inside the menu item.
Ideally I would like to use a SwiftUI View struct if this is at all possible in this case and avoid XIB files altogether.
Update: I tried NSHostingView for integration, but with the same result - the menu item would not display
AppDelegate
func applicationDidFinishLaunching(_ aNotification: Notification) {
self.statusBarItem = NSStatusBar.system.statusItem(withLength:
CGFloat(NSStatusItem.variableLength))
// set the menu and the task bar image
self.statusBarItem.menu = TaskBarMenu()
self.statusBarItem.button?.image = NSImage(named: "icon-normal")
}
TaskbarMenu
class TaskBarMenu: NSMenu {
override init(title: String) {
super.init(title: title)
let testMenu = NSMenuItem()
testMenu.view = TaskbarMenuView()
items = [
NSMenuItem(title:"Test Item", action: #selector(NSApplication.hide(_:)), keyEquivalent: "h"),
NSMenuItem.separator(),
testMenu
]
}
required init(coder: NSCoder) {
super.init(coder : coder)
}
}
TaskbarMenuView
class TaskbarMenuView: NSView {
let nameTextField: NSTextField = {
let textField = NSTextField()
textField.stringValue = "Some custom name"
return textField
}()
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Upvotes: 2
Views: 1573
Reputation: 43
I found the issue: You need to set the frame of the view, otherwise nothing is displayed.
How to create status bar icon & menu with SwiftUI like in macos Big Sur
Upvotes: 1