3

Is possible to put two objects with two different contextMenu in the same row of a Form/List?

Example:

List {
  HStack {
    Image("image.A").contextMenu{ Text("I'm A") }
    Image("image.B").contextMenu{ Text("I'm B") }
  }
}

Similar problem of: SwiftUI - Multiple Buttons in a List row

Lorenzo Fiamingo
  • 3,251
  • 2
  • 17
  • 35

1 Answers1

2

Here is a possible approach. Use a @State to differentiate between the cases and modify it with a onLongPressGesture

Tested on iOS 13.5

struct ContentView: View {
    
    @State var whichImage: Int = 0
    
    var body: some View {
        List {
          HStack {
            Image("image1").resizable().frame(width: 40, height: 40).onLongPressGesture {
                self.whichImage = 1
            }
            Image("image2").resizable().frame(width: 40, height: 40).onLongPressGesture {
                self.whichImage = 2
            }
            
          }.contextMenu{ Text(String(self.whichImage)) }
        }
    }
}

Outcome:

enter image description here

Simon
  • 1,754
  • 14
  • 32