Ibrahim99
Ibrahim99

Reputation: 369

Setting SwiftUI View as NSMenuBarItem view property

I am writing a MacOS Menubar app and I want to assign a SwiftUI view as the NSMenuItem view. Here the current code, but this results in the menu item being hidden.

@IBOutlet weak var firstMenuItem: NSMenuItem?

if let item = firstMenuItem {
    let contentView = NSHostingController(rootView: contentView)
    item.view = contentView.view
}

Upvotes: 1

Views: 460

Answers (1)

Ibrahim99
Ibrahim99

Reputation: 369

I solved this problem by setting the frame for the view

if let item = firstMenuItem {
    let contentView = NSHostingController(rootView: contentView)
    contentView.view.frame.size = CGSize(width: 100, height: 100)
    item.view = mainView.view
}

Upvotes: 2

Related Questions