10

I am developing a macOS app in SwiftUI. I am using .contextMenu modifier inside my List. It all works fine, however I am trying to make a submenu.

Usually I am just using buttons, I am not trying to make a hierarchy / or a submenu. It is possible and being used in many Apple default apps. I just not sure how to create that menu. That's the code I am currently using

.contextMenu {
    Button(action: {
    })
    {
        Text("Button")
    }
}
   

Here is a picture of the default Calendar app, with a sub menu.

davidev
  • 7,694
  • 5
  • 21
  • 56

1 Answers1

16

You can use Menu for nested menu hierarchies:

.contextMenu {
    Menu("Nested Root") {
        Button("Nested #1") {}
        Button("Nested #2") {}
        Button("Nested #3") {}
    }
    Button("Not Nested") { }
}

Kirsteins
  • 27,065
  • 8
  • 76
  • 78
  • Thank you. Unfortunately I need a solution for macOS 10+ and menu is only available from 11+. However, I think there is no other solution yet. I will accept that answer – davidev Oct 09 '20 at 15:05
  • I'm on iOS 13 we don't have a Menu here – Lukasz D Nov 24 '21 at 08:33