假设我List在一行中有一个和两个按钮,如何在不突出显示整个行的情况下区分哪个按钮被轻拍了?
List
对于此示例代码,当点击该行中的任何一个按钮时,将同时调用两个按钮的动作回调。
// a simple list with just one row List { // both buttons in a HStack so that they appear in a single row HStack { Button(action: { print("button 1 tapped") }) { Text("One") } Button(action: { print("button 2 tapped") }) { Text("Two") } } } // when tapping just once on either button: // "button 1 tapped" // "button 2 tapped"
您需要使用 BordlessButtonStyle() 。
List([1, 2, 3], id: \.self) { row in HStack { Button(action: { print("Buton at \(row) with name A") }) { Text("Row: \(row)" + " Name: A") }.buttonStyle(BorderlessButtonStyle()) Button(action: { print("Buton at \(row) with name B") }) { Text("Row: \(row)" + " Name: B") }.buttonStyle(BorderlessButtonStyle()) } }