0

In my code, I have a Search Bar on the top that have a "magnifyingglass" inside a circle as a button. When I click it, it shows a textfield, and then I have to press it to open the keyboard. What do I need to implement in the code so that when I click on the "magnifyingglass" image it immediately opens the keyboard, without having to press it again?

VStack(spacing: 0) {
        
        HStack{
                         
                         if !self.show{
                             
                              Text("Buscar")
                                 .fontWeight(.bold)
                                 .font(.title)
                                 .foregroundColor(.white)
                         }
                         
                         Spacer(minLength: 0)
                         
                         HStack{
                             
                             if self.show{
                                 
                                Image(systemName: "magnifyingglass")
                                    .padding(.leading, 5)
                                 
                                TextField("Buscar", text: self.$txt)
                                    
                                    
                                 
                                 Button(action: {
                                     
                                     withAnimation {
                                         
                                         self.txt = ""
                                         self.show.toggle()
                                     }
                                     
                                 }) {
                                     
                                     Image(systemName: "xmark").foregroundColor(.black)
                                 }
                                 .padding(.horizontal, 8)
                                
                            }
                             
                             else{
                                 
                                 Button(action: {
                                     
                                     withAnimation {
                                         
                                         self.show.toggle()
                                     }
                                     
                                 }) {
                                     
                                     Image(systemName:"magnifyingglass")
                                        .resizable()
                                        .frame(width: 22, height: 22)
                                        .font(Font.system(.body).bold())
                                        .foregroundColor(.black)
                                        .padding(10)
                                     
                                 }
                             }
                         }
                         .padding(self.show ? 10 : 0)
                         .background(Color.white)
                         .cornerRadius(90)
                        
                         
                         
                     }
                     .padding(.top, UIApplication.shared.windows.first?.safeAreaInsets.top == 0 ? 0 : UIApplication.shared.windows.first?.safeAreaInsets.top)
                     .padding(.horizontal)
                     .padding(.bottom , 10)
                     .background(Color("Color"))
shim
  • 9,289
  • 12
  • 69
  • 108
Mateus Neves
  • 674
  • 1
  • 6
  • 20
  • The context is slightly different, but maybe this helps you: https://stackoverflow.com/questions/56507839/swiftui-how-to-make-textfield-become-first-responder – cbjeukendrup Aug 11 '20 at 16:01

1 Answers1

0

EDIT: Sorry, becomeFirstResponder isn't available at the moment on SwiftUI, according to this answer you could achieve the same behavior by implementing something similar yourself. https://stackoverflow.com/a/56508132/5848719

TheNewKid
  • 201
  • 4
  • 7