Skip to content

Instantly share code, notes, and snippets.

@kmalyshev
Created January 11, 2021 20:26
Show Gist options
  • Save kmalyshev/7ef834efaeed83014f6ba851582fca0d to your computer and use it in GitHub Desktop.
Save kmalyshev/7ef834efaeed83014f6ba851582fca0d to your computer and use it in GitHub Desktop.
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
let contentView = VStack {
Button("TestButton", action: {
print("testest")
})
}
let view = NSHostingView(rootView: contentView)
view.frame = NSRect(x: 0, y: 0, width: 200, height: 200)
let menuItem = NSMenuItem()
menuItem.view = view
let menu = NSMenu()
menu.addItem(menuItem)
statusItem.menu = menu
statusItem.button?.title = "Test"
@kmalyshev
Copy link
Author

image
image

@makhocheung
Copy link

image

Hello.

thanks your answer here
And Do you know what control it is in the picture above ? It hovers with a translucent background and I am sure it is not a button.
Thanks.

@kmalyshev
Copy link
Author

Hello.
thanks your answer here And Do you know what control it is in the picture above ? It hovers with a translucent background and I am sure it is not a button. Thanks.

Hello @makhocheung,
You should try using Accessibility Inspector, it reveals what's being used under the hood.

137863136-e6dfe6d6-8c6b-49e0-b3bd-a68e4cfbb936

Also please check this topic about regular NSMenuItems being highlighted in a custom way. https://stackoverflow.com/questions/6054331/highlighting-a-nsmenuitem-with-a-custom-view

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment