#this is just an example of the code so when i longpress the "Text("Long press for menu")" and is showing the contextmenu to change the background color:
struct ContentView: View {
var body: some View {
Text("Long press for menu")
.contextMenu {
Button {
print("Pills selected")
} label: {
Label("Pills", systemImage: "pills")
}
Button {
print("Heart selected")
} label: {
Label("Heart rate", systemImage: "heart")
}
Button {
print("ECG selected")
} label: {
Label("ECG", systemImage: "waveform.path.ecg")
}
}
}
}```
2
Answers
The issue is the contextMnenu is creating a rectangle around the text, and this rectangle seems to hate transparency like Color.clear or opacity applied to white. As I’ve become accostumed to say "SwiftUI never cease to suprprise me (both in good and bad ways)". So you need to build your custom menu for that. You can get away with something like the following:
}
Let me know if that helped!
You can change the color using preview for the .contextMenu:
Here is the result (I just added .padding modifier to make this view more appeal):