How to change the background color of the content when is long pressed and is showing the contextMenu?

55 Views Asked by At

enter image description here

This is just an example of how ChatGPT transitions from a clear background to a dark gray background for the content of the message when contextMenu is visible. Here are my codes:

 struct ContentView: View {
   var body: some View {
        Text("Long press for menu")
            .background(.clear)
            .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")
                }
            }
    }
1

There are 1 best solutions below

0
narek.sv On

iOS 16.0+

You can provide your own preview to the contextMenu like this:

struct ContentView: View {
    var body: some View {
        Text("Long press for menu")
            .contextMenu {
                // Buttons
            } preview: {
                Text("Preview")
                    .background(Color.red)
            }

    }
}

For older iOS versions you need a custom implementation: Something like this.