Search code examples
swiftwatchkitapple-watchswiftui

SwiftUI - Apple Watch Menu (Force Touch)


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.


Solution

  • 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.