TechnologiesSwiftUIPickers and Text Fields

DefaultTextFieldStyle struct

iOSmacOStvOSwatchOSvisionOSiOS 13.0+✓ renders

The default text field style, based on the text field's context.

How it works

DefaultTextFieldStyle is the text field style SwiftUI applies automatically when you don't request a specific one. It defers to the platform and surrounding context, drawing each TextField the way the system would in that environment so your forms feel native on iOS, macOS, and elsewhere without per-platform code. Reach for it explicitly when you want to opt a particular field back into system styling—for example, to override an inherited style set higher in the view hierarchy.

  1. Conform to TextFieldStyle through DefaultTextFieldStyle

    DefaultTextFieldStyle is a concrete struct that conforms to the TextFieldStyle protocol, the type that describes how a TextField is rendered. Because it is the standard conformance, it carries the platform's built-in appearance and interaction rather than a custom look. In the example, DefaultTextFieldStyle() is the value standing in for whatever the current platform considers normal for a TextField.

  2. Create an instance with the no-argument initializer

    You construct the style with its parameterless initializer, DefaultTextFieldStyle(). There are no options to configure—the type intentionally has no tunable parameters, since its whole purpose is to hand styling decisions back to the system and the surrounding context.

  3. Apply it with textFieldStyle(_:)

    A style only takes effect once it's attached to a field through the textFieldStyle(_:) modifier, which accepts any TextFieldStyle. Here .textFieldStyle(DefaultTextFieldStyle()) is chained onto the TextField("Full name", text: $name), telling that field to render in the default manner.

  4. Use it to reset an inherited style

    Because textFieldStyle(_:) propagates down the view tree, a field can inherit a non-default style from an ancestor. Passing DefaultTextFieldStyle() explicitly on the TextField restores system styling for that field alone, which is the main reason to name the default rather than rely on it implicitly.

Try it — Swap DefaultTextFieldStyle() in the .textFieldStyle(...) modifier for .roundedBorder to see how the same field departs from the platform default and gains a visible bordered frame.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

DefaultTextFieldStyle.swift
struct DefaultTextFieldStyleDemo: View {
    @State private var name = ""
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Profile")
                .font(.headline)
            TextField("Full name", text: $name)
                .textFieldStyle(DefaultTextFieldStyle())
            Text("Hello, \(name.isEmpty ? "stranger" : name)!")
                .foregroundColor(.secondary)
        }
        .padding()
    }
}
Live preview
Profile Full name Hello, !
Profile Full name Hello, !
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →