LAST CHANCE: Save 50% on all my Swift books and bundles! >>

Word Scramble challenge: How to regain focus in TextField

Forums > 100 Days of SwiftUI

After having clicked a "Start a new game" Button as a NavigationBarItem I notice that the focus goes from the TextField.

A brief Googling for a solution to impose some kind of "Tab Index" or forcing focus on a particular View leads me to believe that there is no simple SwiftUI answer. Am I right?

3      

I don't believe there is a way to do it with pure SwiftUI but I'm still new to the language (day 75/100) so I might be wrong.

I did however find this is a swift package that can give SwiftUI elements access to features from their UIKit or AppKit equivalents: SwiftUI-Introspect

On the github page, click the "Clone or Download" button then copy the https link provided. It should be this one: https://github.com/siteline/SwiftUI-Introspect.git

To add this package to your project in XCode, go to: File > Swift Packages > Add Package Dependency then paste the link.

You should see the package appear on the left column below your files and under the heading "Swift Package Dependencies"

Now that the package is in your project, you need to add this line to use it in one of your swift files. Add this to ContentView.swift

import Introspect

Now you can add a new vew modifier to your text field to make it act as a first responder.

.introspectTextField { textField in
    textField.becomeFirstResponder()
}

The .becomeFirstResponder() function is from UIKit and it forces the text field to become active every time the view is refreshed.

I would also recommend adding this to your text field to prevent it from activating while alerts are showing.

.disabled(showingError)

Keep in mind this solution doesn't seem to work if you have more than one text field as the focus will keep jumping back to the first whenever the view reloads.

5      

Very similar question where I suspect the answer is similar. I was hoping to keep the TextField active after a word is submitted rather than forcing the user to tap the TextField again to bring up the keyboard.

Looks like there are some updates for handling focus in Xcode 12...

3      

Could you elaborate on the note about handling focus in Xcode 12?

3      

With UIkit it was easy to make a Textfield to be FirstResponder. In SwiftUI there ist (as far as I know) up to now still no simple way. You have to use UIkit inside SwiftUI. The simplest way for me was to use the package "Intospect" : https://github.com/timbersoftware/SwiftUI-Introspect.git

3      

For anybody having problems using @ryanlintott 's solution, I solved mine by using the modifier on the parent class.

For example, I was using Ryan's code like this:

TextField("Placeholder text...", text: $searchInput)
    .introspectTextField { textField in
        textField.becomeFirstResponder()
    }

To solve the problem, I stopped using the modifier directly on the TextField:

       TopSearchSection(searchInput: $searchInput)
           .introspectTextField { textField in
                              textField.becomeFirstResponder()
           }

       // I'll put the definition of the struct below just for clearness 
       struct TopSearchSection: View {
           @Binding var searchInput: String

           var body: some View {
               HStack {
                   TextField("Placeholder text...", text: $searchInput)                      
                      .padding()
                      .background(Color.gray)
                      .cornerRadius(10)
               }
           }
       }

3      

This should be solvable very easily in SwiftUI 3/Xcode 13 using the new @FocusState property wrapper.

4      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.