Is it possible to have multiple independent context menus within a list row without these being displayed together as on menu?
I put together a small example to illustrate the question. The desired behavior is that each of the two Text(...) views have their own contextmenu, which is only displayed when pressing that text.
In the example, I can long press anywhere on the list row and will be presented with a context menu with 4 choices, the two menus appended to each other.
struct ContentView: View
{
let data = [
"Test 1","Test 2","Test 3","Test 4","Test 5",
"Test 6","Test 7","Test 8","Test 9","Test 10",
"Test 11","Test 12","Test 13","Test 14","Test 15",
"Test 16","Test 17","Test 18","Test 19","Test 20"
]
var body: some View
{
List
{
ForEach(data,id:\.self)
{
item in
HStack
{
Text(item+" A")
.contextMenu
{
Button(action: {}) { Text("Item one"); Image(systemName: "globe") }
Button(action: {}) { Text("Item two"); Image(systemName: "location.circle") }
}
Spacer()
Text(item+" B")
.contextMenu
{
Button(action: {}) { Text("Item one"); Image(systemName: "globe") }
Button(action: {}) { Text("Item two"); Image(systemName: "location.circle") }
}
}
}
}
}
}
Yes, one problem is longpress is detected at HStack level, not differently in each Text() view ; even the spacer area reacts to longPress.
But if you replace List by VStack, you get what you want.
struct ContentView: View
{
let data = [
"Test 1","Test 2","Test 3","Test 4","Test 5",
"Test 6","Test 7","Test 8","Test 9","Test 10",
"Test 11","Test 12","Test 13","Test 14","Test 15",
"Test 16","Test 17","Test 18","Test 19","Test 20"
]
var body: some View
{
VStack
{
ForEach(data,id:\.self)
{
item in
HStack
{
Text(item+" A ")
.background(Color.blue)
.contextMenu
{
Button(action: {}) { Text("Item one A \(item)"); Image(systemName: "globe") }
Button(action: {}) { Text("Item two A \(item)"); Image(systemName: "location.circle") }
}
Spacer()
Text(item+" B ")
.background(Color.red)
.contextMenu
{
Button(action: {}) { Text("Item one B \(item)"); Image(systemName: "globe") }
Button(action: {}) { Text("Item two B \(item)"); Image(systemName: "location.circle") }
}
}
.frame(width: 300, height: 40)
}
}
}
}