Autofocus TextField programmatically in SwiftUI : <p>I'm using a modal to add names to a list. When the modal is shown, I want to focus the TextField automatically, like this:</p>

<p><a href="https://i.stack.imgur.com/gkmeA.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/gkmeA.gif" alt="Preview"></a></p>

<p>I've not found any suitable solutions yet.</p>

<p>Is there anything implemented into SwiftUI already in order to do this?</p>

<p>Thanks for your help.</p>

<pre><code>var modal: some View {
        NavigationView{
            VStack{
                HStack{
                    Spacer()
                    TextField("Name", text: $inputText) // autofocus this!
                        .textFieldStyle(DefaultTextFieldStyle())
                        .padding()
                        .font(.system(size: 25))
                        // something like .focus() ??
                    Spacer()
                }
                Button(action: {
                    if self.inputText != ""{
                        self.players.append(Player(name: self.inputText))
                        self.inputText = ""
                        self.isModal = false
                    }
                }, label: {
                    HStack{
                        Text("Add \(inputText)")
                        Image(systemName: "plus")
                    }
                        .font(.system(size: 20))
                })
                    .padding()
                    .foregroundColor(.white)
                    .background(Color.blue)
                    .cornerRadius(10)
                Spacer()
            }
                .navigationBarTitle("New Player")
                .navigationBarItems(trailing: Button(action: {self.isModal=false}, label: {Text("Cancel").font(.system(size: 20))}))
                .padding()
        }
    }
</code></pre>
