Using AppKit you can add
NSMenuItem
s to an
NSMenu
. I saw that there is something similar to NSMenu in SwiftUI, namely
MenuButton
. But I cannot find any documentation on how it works.
I tried the following:
MenuButton("+") {
Button("New contact") { print("Create new contact") }
Button("New group") { print("Create new group") }
}
And that looks almost OK but
- The buttons have a different background color than the menu (notice the slightly lighter color above and beneath the menu items).
- When I hover the menu items, their background color doesn't change like a normal macOS menu.
I also tried to change the background color manually using the
.background()
modifier but that doesn't affect the full width of the menu item.
I suppose this is because I am placing
Button
s inside the
MenuButton
while it is probably expecting some other SwiftUI element. What elements should I place inside MenuButtons to create a normal-looking macOS menu?
See this stackoverflow post for more detailed images that describe what I mean: https://stackoverflow.com/questions/59837991/how-to-add-menu-items-to-a-menubutton-in-swiftui