Context menu in a SwiftUI app?

I'm trying to configure a force touch context menu for a SwiftUI watchOS project. Normally I'd use WKInterfaceController's addMenuItem method, but I'm at a loss as to how to get this to work with SwiftUI. Has anyone out there had success with this?

Accepted Reply

An attempt of mine looks like this:


VStack {

...

}

.contextMenu {

Button(action: { print("Start") } ) {

Text("Start")

}

}


My Problem here is that I haven't found out how to use UIImages in there yet.


I hope it helps!

Replies

An attempt of mine looks like this:


VStack {

...

}

.contextMenu {

Button(action: { print("Start") } ) {

Text("Start")

}

}


My Problem here is that I haven't found out how to use UIImages in there yet.


I hope it helps!

That's great, so much neater than before!


I was able to get images from an Asset Catalog to show up by adding a VStack to the button label:


        .contextMenu {
            Button(action: { print("tapped") },
                  label: {
                    VStack {
                        Image("image_name")
                        Text("label")
                    }
            })
        }



or you can use an SF Symbol:


VStack {
    Image(systemName: "exclamationmark.bubble.fill")
    Text("label")
}

Thank you so much!

I was wonderint how to add an image, too, and this helped me solve the issue.

it's quite strange that the behavior is so different between iOS and watchOS, but whatever, it will probably get fixed in a next beta