Reputation: 12079
I have a SwiftUI Text
view in my app, and I would like to display a UIMenuController or its SwiftUI equivalent whenever I tap on it. I can capture the tap action successfully by adding an onTapGesture
block to my View, but I haven't been able to find any examples showing how to display the menu. Is there a SwiftUI equivalent to UIMenuController
? Do I need to create the whole UIViewRepresentable thing for it?
Upvotes: 3
Views: 787
Reputation: 150735
There is a contextMenu
auxiliary view in SwiftUI
struct ContentView: View {
@State private var fave = "?"
var body: some View {
VStack {
Text("Favorite Card Suit")
.padding()
.contextMenu {
Button("♥️ - Hearts", action: selectHearts)
Button("♣️ - Clubs", action: selectClubs)
Button("♠️ - Spades", action: selectSpades)
Button("♦️ - Diamonds", action: selectDiamonds)
}
Text("Your favourite is")
Text(fave)
}
}
func selectHearts() {
fave = "♥️"
}
func selectClubs() {
fave = "♣️"
}
func selectSpades() {
fave = "♠️"
}
func selectDiamonds() {
fave = "♦️"
}
}
You don't even have to listen for taps, just a long press brings up a the menu.
Upvotes: -3