SwiftUI can't tap in Spacer of HStack SwiftUI can't tap in Spacer of HStack swift swift

SwiftUI can't tap in Spacer of HStack


As I've recently learned there is also:

HStack {  ...}.contentShape(Rectangle()).onTapGesture { ... }

Works well for me.


Why not just use a Button?

Button(action: { self.groupSelected(self.group) }) {    HStack {        Text(group.name)        Spacer()        if (groupModel.required) { Text("Required").color(Color.gray) }        Image("ic_collapse").renderingMode(.template).rotationEffect(Angle(degrees: 90)).foregroundColor(Color.gray)    }}.foregroundColor(.primary)

If you don't want the button to apply the accent color to the Text(group.name), you have to set the foregroundColor as I did in my example.


works like magic on every view:

extension View {        func onTapGestureForced(count: Int = 1, perform action: @escaping () -> Void) -> some View {            self                .contentShape(Rectangle())                .onTapGesture(count:count, perform:action)        }    }