LAST CHANCE: Save 50% on all my Swift books and bundles! >>

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 Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

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.