Focus TextField MacOS SwiftUI

Viewed 119

I am trying to auto select a text field for a MacOS application. I added .focusable() but this does not change the result and the text field is still not auto selected and I need to click onto the text field to start typing.

...
TextField("text", text: $inputText)
   .focusable()
...
1 Answers

@FocusState is the property wrapper you need. Set the focus variable with your custom type. You can set this onAppear or based on any other conditions

  enum Focus : Hashable  
{
  case some             
}  
   
  @FocusState var focus : Focus?



TextField()
.focused($focus, equals: .some)
Related