macosswiftuimenu

Style a plain dropdown menu in SwiftUI


I’m trying to create a simple dropdown button in SwiftUI on macOS. Normally I would expect to use something like this:

MenuButton("☰") {
    Button(action: { print("Something") }) {
        HStack {
            Image(systemName: "questionmark.circle")
            Text("Something")
        }
    }
    Button(action: { print("Something Else") }) {
        HStack {
            Image(systemName: "exclamationmark.circle")
            Text("Something Else")
        }
    }
}
.menuButtonStyle(BorderlessButtonMenuButtonStyle())

The .menuButtonStyle(BorderlessButtonMenuButtonStyle()) shows only the menu icon (☰)

enter image description here

without the drop down

enter image description here

However, MenuButton has been deprecated in favour of Menu, which is fine, but I then need to modify it with .menuStyle, and can’t find the equivalent of BorderlessButtonMenuButtonStyle

What would be the equivalent for Menu to suppress the drop down arrow?


Solution

  • As per the documentation (which may be somewhat innacurate since .buttonStyle(.borderless) does display a down arrow):

    borderlessButton Deprecated

    Use menuStyle(_:) with button and buttonStyle(_:) with borderless.

            Menu("☰") {
                    Button(action: { print("Something") }) {
                        HStack {
                            Image(systemName: "questionmark.circle")
                            Text("Something")
                        }
                    }
                    Button(action: { print("Something Else") }) {
                        HStack {
                            Image(systemName: "exclamationmark.circle")
                            Text("Something Else")
                        }
                    }
                }
                .menuStyle(.button)
                .buttonStyle(.plain)