WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Is there a way in SwiftUI to disable initial focus on a TextField?

Forums > macOS

Normally you open a sheet to display a view to enter or edit some info and having the first field automatically get focus so it is ready for input is great and just what you want. However, I find myself in a situation where I would like to open a sheet with several TextFields that have been filled with existing info and I don't want any of the fields to automatically get focus (so the user doesn't accidentally start to type and replace any of whatever has been autoselected). I only want the user to be able to edit if they specifically hit tab or click into one of the fields to actually start editing.

Is there a way to disable the initial focus in SwiftUI? I tried putting in the @FocusState and .focused modifiers on each field and then setting the @FocusState variable to nil .onAppear if a certain variable is true (the window has been opened with the existing info). The focus stubbornly appears on the first field. Yet if I set the focus to another field in the .onAppear then that actually works.

   

Well this gets curiouser and curiouser...

Apparently it just doesn't like to set the @FocusState variable specifically to nil in the .onAppear, but I found that if I set up the following at the end of .onAppear:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    hasFocus = nil
}

Then it will work to remove the focus after very briefly displaying the focus. Fortunately one of the fields in the sheet I'm opening is a TextEditor and if I set the hasFocus to that first in the .onAppear the cursor just blinks once after the text (if any) and then stops. No focus ring and text highlighting like on a TextField which is a lot more obvious.

Still, it is annoying that I can't seem to get it to just not have the focus to begin with. Maybe there is some other way or some other syntax I've missed?

   

Perhaps you can eliminate the blink by initially setting the focus to a "dummy" text field that is hidden. For example, it can be in a ZStack behind a rectangle whose color matches the background color so that both are effectively invisible. After 0.1 sec you can reset hasFocus to nil.

   

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.