13

I'm trying to implement a menu on Apple Watch using SwiftUI but I can't find a way to do it. Even on the interface.storyboard, I can't drag/drop the menu.

Did you manage to make it work with SwiftUI? If yes, how?

I searched online but nothing so far.

Daymo502
  • 825
  • 1
  • 6
  • 10

1 Answers1

20

Yes, this is possible. It's important to remember that unlike on iOS, a view can have only one single context menu, individual elements within the view can not have their own context menu.

Anyway, to implement a context menu (force touch menu) on Apple Watch with SwiftUI, add the .contextMenu() modifier to top-most view in your body

Example:

var body: some View {
    Group {
        Text("Hello Daymo")
    }
    .contextMenu(menuItems: {
        Button(action: {
            print("Refresh")
        }, label: {
            VStack{
                Image(systemName: "arrow.clockwise")
                    .font(.title)
                Text("Refresh view")
            }
        })
    })
}

Edit the button (or add buttons) as you see fit.

Will
  • 4,942
  • 2
  • 22
  • 47