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?
Asked
Active
Viewed 657 times
3

Shadowman
- 11,150
- 19
- 100
- 198
1 Answers
-3
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.

Abizern
- 146,289
- 39
- 203
- 257
-
2This is not what was asked – Maciej Swic May 21 '22 at 04:33
-
1People don't read the questions – iflorit Aug 10 '22 at 12:58