TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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 Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.