Home > Blockchain >  swiftui: textfield in HStack, when tap the textfield, the code in ontapgesture of HStack is also exe
swiftui: textfield in HStack, when tap the textfield, the code in ontapgesture of HStack is also exe

Time:04-29

HStack {
   textfield("", text: $text)
   Text("Something")
}
.onTapGesture {
   code
}

How can I focus the textfield without executing the codes in onTapGesture? please help me, thanks a lot.

CodePudding user response:

I come with following idea, it's simple and maybe temporary solution. Just Add onTapGesture Action on TextField so it will execute that and does not execute action on HStack.

       HStack {
            TextField("", text: $text).onTapGesture {
                print("Tap On TextField")
            }
           Text("Something")
        }
        .onTapGesture {
           print("Tap On HStack")
        }

If we tap on Text("Something") it will execute HStack onTapGesture Action.

Update : -

WE can also do something like following :

    HStack {
        TextField("", text: $text).onTapGesture {
            print("Tap On TextField")
        }
       Group {
            Text("Something")
            // Add Other Item here
       }.onTapGesture {
            print("Tap On Group")
       }
    }
        

This simply make 2 groups in Hstack and on Textfield Tap if you don't want any action then simply don't put any action on it. Put Your action on Other Group.

  • Related