A blog about Swift and iOS tricks

Enable tapping on the entire SwiftUI stack view, including spacers

HStack {
	Button()
	Spacer()
	Image()
}
.contentShape(Rectangle())
.onTapGesture {
 // all the stack view is tapable now
}

or we can put it in an extension


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