Learning Time : 1h 40min

My Github : Hot Prospects Source Code

My Note index link

day81.png

KeyWords

Creating context menus

Context menus provide users with additional functionality when they press and hold on a view, similar to 3D Touch on older iPhones.

contextMenu(menuItems:) | Apple Developer Documentation

Example

struct ContentView: View {
    @State private var backgroundColor = Color.red

    var body: some View {
        VStack {
            Text("Hello, World!")
                .padding()
                .background(backgroundColor)

            Text("Change Color")
                .padding()
                .contextMenu {
                    Button("Red") {
                        backgroundColor = .red
                    }

                    Button("Green") {
                        backgroundColor = .green
                    }

                    Button("Blue") {
                        backgroundColor = .blue
                    }
                }
        }
    }
}

Tips

  1. If you’re going to use them, use them in lots of places – it can be frustrating to press and hold on something only to find nothing happens.