10

Case 1:- When I have multiple Buttons in a VStack, on clicking of any one of them, the action handlers of both the buttons executes immediately, this happens only when the VStack is a child of List. For eg-

List { VStack { Button(action: { print("A") }) { Text("Button A") } Button(action: { print("B") }) { Text("Button B") } } } 

Here when you click on any one Button(say Button B), the o/p is:- A B

Case 2:- Try that with just a VStack, it works fine. For eg-

VStack { Button(action: { print("C") }) { Text("Button C") } Button(action: { print("D") }) { Text("Button D") } } 

Here when you click on any one Button(say Button D), the o/p is:- D

I am new to iOS programming, please help me understand where I am going wrong or is it an issue with SwiftUI?

2 Answers 2

12

Set the button style to something different from the default, e.g., BorderlessButtonStyle()

struct Test: View { var body: some View { NavigationView { List { ForEach([ "Line 1", "Line 2", ], id: \.self) { item in HStack { Text("\(item)") Spacer() Button(action: { print("\(item) 1")}) { Text("Button 1") } Button(action: { print("\(item) 2")}) { Text("Button 2") } } } .onDelete { _ in } .buttonStyle(BorderlessButtonStyle()) } .navigationBarItems(trailing: EditButton()) } .accentColor(.red) } } 
Sign up to request clarification or add additional context in comments.

6 Comments

Yes i tested your suggestion and also few other ButtonStyles and found that only DefaultButtonStyle has this issue. If we apply other Styles to Button it works fine. Still not sure if it's a bug or intended behaviour. Thanks though.
Changing the button style also solved my issue.
This bug is still exist in iOS 16.2...
Bug still exists today with latest version.
latest iOS 16.4 and this is still around, almost freaked out from this
|
2

Buttons inside List won't work as expected. Most likely a bug, but not acknowledge officially. Since it wasn't acknowledge, nobody can tell when/if it's going to be fixed.

In the meantime, you may use a custom button, like the one below. It replicates the Button behavior (color and dimming on tap):

struct ContentView: View { @State private var flag = false var body: some View { List { VStack { MyButton(label: "Button A") { print("A") } MyButton(label: "Button B") { print("B") } } } } } struct MyButton: View { @State private var tapped = false let label: String let action: () -> () var body: some View { let g = DragGesture(minimumDistance: 0, coordinateSpace: .local) .onChanged({ _ in withAnimation { self.tapped = true } }) .onEnded({ _ in withAnimation { self.tapped = false } self.action() }) return Text(label) .foregroundColor(Color(UIColor.link) .opacity(tapped ? 0.5 : 1.0)) .gesture(g) } } 

3 Comments

I know the workaround, just wanted to know if this is an issue with SwiftUI(which'll be fixed later) or there's something I don't know conceptually. Also the Buttons doesn't hold their Blue colour appearance when inside a List and looks just like a simple Text.
I updated the answer, to include a working button (color and dimming on tap included.
Also note that it is most likely a bug, but not acknowledge officially. Since it wasn't acknowledge, nobody can tell when/if it's going to be fixed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.