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.
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 aTextField.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.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 theTextField("Full name", text: $name), telling that field to render in the default manner.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 theTextFieldrestores system styling for that field alone, which is the main reason to name the default rather than rely on it implicitly.
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.
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()
}
}