UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Tap button in HStack activates ALL button actions (iOS 14, SwiftUI 2)

Forums > SwiftUI

@frios  

I have multiple buttons in an HStack in SwiftUI 2 (iOS 14). When i tap on a single button, the whole stack gets highlighted and the actions for ALL of the buttons gets executed.

struct ActionsView: View {
    var truckDetails: TruckDetails
    @State var selectedTag: String?

    var body: some View {
        HStack (alignment: .bottom) {
            Button(action: {
                if let url = URL(string: "tel://\(truckDetails.phone)"), UIApplication.shared.canOpenURL(url) {
                        UIApplication.shared.open(url)
                }
            }, label: {
                VStack {
                    Image(systemName: "phone")
                    Text("Call")
                }
            })
            .disabled(truckDetails.phone == "")

            Spacer()

            Button(action: {
                self.selectedTag = "menus"
            }, label: {
                VStack {
                    Image(systemName: "line.horizontal.3")
                        .padding(.bottom, 3)
                    Text("Menu")
                }
            })
            .disabled(!truckDetails.menus)
            .background(
                NavigationLink(
                    destination: Text("Menus"),
                    tag: "menus",
                    selection: $selectedTag,
                    label: {
                        EmptyView()
                    })
                    .buttonStyle(PlainButtonStyle())
            )

            Spacer()

            Button(action: {
                selectedTag = "schedules"
            }, label: {
                VStack {
                    Image(systemName: "calendar")
                    Text("Schedule")
                }
            })
            .disabled(!truckDetails.schedules)
            .background(
                NavigationLink(
                    destination: Text("Schedules"),
                    tag: "schedules",
                    selection: $selectedTag,
                    label: {
                        EmptyView()
                    })
                    .buttonStyle(PlainButtonStyle())
            )

            Spacer()

            Button(action: {

            }, label: {
                VStack {
                    Image(systemName: "heart")
                    Text("Favorite")
                }
            })
        }
        .padding(.horizontal)
        .font(.footnote)

    }
}

So if I tap on the Call button not only does it bring up the Calling UI but it navigates to the Schedules Text (second option.)

Is this a Swift 2 bug that I should be filing a Feedback on or am I doing something wrong here?

5      

@frios  

Was able to put this view into an XCode 11 proect and it operates the same way. So the question is, how to I limit the execution to the tapped button's action when there are multiple buttons in an HStack? I tried setting the .contentShape (Rectangle()) on each button but that didn't help.

3      

@frios  

And the answer is...

.buttonStyle(BorderlessButtonStyle())

14      

I just lost 3 days because because the action triggert a backend function and I was looking there! Thanks so much

3      

@frios thankyou thankyou! head scratching can now stop!

3      

I almost went down a rabbit hole with this problem. Happy that it has a quick fix. Thank you.

3      

THANK YOU!

3      

Still the correct answer with Swift 5.5 and SwiftUI 3!

Thank you!!

3      

.buttonStyle(BorderlessButtonStyle())

HOW did you find this?? You just made my day! Thank you !!!!

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.