marcus
marcus

Reputation: 83

Focus TextField MacOS SwiftUI

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()
...

Upvotes: 1

Views: 331

Answers (1)

arthas
arthas

Reputation: 808

@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)

Upvotes: 1

Related Questions