I am a swiftui beginner, and there is a problem that has been bothering me. Multiple buttons on the same list item, why does the contextmenu not take effect? I want the button have the context menu not list item. the code is below:
struct TestView: View {
var body: some View {
List{
Section("标签"){
HStack(alignment: .top,spacing: 5) {
Text("hello")
.contextMenu(menuItems: {
Button(role:.destructive) {// not working
// todo add action code
} label: {
Label("删除1", systemImage: "trash")
}
})
.padding(5)
.background(.blue)
.foregroundColor(.white)
.cornerRadius(5.0)
Text("world")
.contextMenu(menuItems: { // not working
Button(role:.destructive) {
// todo add action code
} label: {
Label("删除2", systemImage: "trash")
}
})
.padding(5)
.background(.blue)
.foregroundColor(.white)
.cornerRadius(5.0)
}
}
}
}
}
enter image description here
I want button of “hello” and “world” have contextmenu property , when user long press different button have different menu, even if the two buttons are in the same list item
紫微天和 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.