I want to perform some code when the form is tapped. However the button in the form is affected by the onTapGesture either. I want the button tapped to execute the action block, not the onTapGesture block. How should I do? Thanks in advance.
struct TextFieldPopupView: View {
@State private var text = ""
@State private var isON = false
var body: some View {
Form{
Text("Hello")
TextField("Hello", text: $text)
.textFieldStyle(RoundedBorderTextFieldStyle())
Button(action: {
print("Button")
}, label: {Text("Button")})
Toggle(isOn: $isON, label: {
Text("Toggle")
})
}
.onTapGesture {
print("Form")
}
}
}
here are a few ideas. Since you also have the ".onTapGesture" on the Form itself, it will also be called. So you will have to use some flag to determine which is being called.
struct TextFieldPopupView: View {
@State private var text = ""
@State private var isON = false
var body: some View {
Form {
Text("Hello")
TextField("Hello", text: $text).textFieldStyle(RoundedBorderTextFieldStyle())
Button("Button1"){
print("Button1")
}.buttonStyle(BorderlessButtonStyle())
Text("Button2").foregroundColor(.blue)
.onTapGesture {
print("Button2")
}
Button("Button3"){}
.onTapGesture {
print("Button3")
}
Toggle(isOn: $isON, label: {
Text("Toggle")
})
}
.onTapGesture {
print("---> Form")
}
}
}