Skip to content

SwiftUI forms: TextField

New Course Coming Soon:

Get Really Good at Git

The first form control we’ll see is the simplest: TextField.

This lets us show some text, like the Text view, and it can be editable by the user, so we can get input in the form of text.

Here’s the most basic example of TextField:

struct ContentView: View {
    @State private var name = ""
    
    var body: some View {
        Form {
            TextField("", text: $name)
        }
    }
}

We have a SwiftUI: properties that we prefaced with the @State property wrapper.

Run the code. You can see an empty text field. You can tap on it:

And you can enter any text inside it:

The first argument of TextField is a string that’s visualized when the field is empty. You can fill it with any text you want, like this:

TextField("Your name", text: $name)

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching May 21, 2024. Join the waiting list!
→ Get my Swift Handbook

Here is how can I help you: