TechnologiesSwiftUI

DefaultButtonStyle struct

iOSmacOStvOSwatchOSvisionOSiOS 13.0+✓ renders

The default button style, based on the button's context.

How it works

DefaultButtonStyle is the button style SwiftUI applies when you haven't specified one of your own — it produces the standard appearance and interaction behavior appropriate to the current context, platform, and enclosing container. Because it adapts to its surroundings, a button styled this way looks like a plain control inline but takes on the prominent, bordered treatment expected inside something like an alert or a form. Reach for DefaultButtonStyle when you want to opt back in to system-provided styling explicitly, or to reset a button to the default after an ancestor view has imposed a different style.

  1. Conform through ButtonStyle and PrimitiveButtonStyle

    DefaultButtonStyle is a concrete type that conforms to the button-style protocols, so it slots into the same styling machinery as every other built-in style. You never implement its body yourself; SwiftUI supplies the context-appropriate rendering, which is why a Button labeled "Save Changes" picks up standard system styling without any further description.

  2. Construct it with DefaultButtonStyle()

    The type has a simple no-argument initializer because it carries no configuration of its own — all of its behavior comes from the environment it resolves against. Writing DefaultButtonStyle() gives you an instance whose appearance is determined at render time by the platform and the surrounding container.

  3. Apply it with the buttonStyle(_:) modifier

    You attach the style by passing the instance to .buttonStyle(DefaultButtonStyle()) on a Button. The modifier sets the style for that button and any descendant buttons that don't override it, so it's also the tool you use to restore default styling beneath an ancestor that set a different one.

  4. Prefer the .automatic shorthand

    SwiftUI exposes the same default behavior through the .automatic static member, letting you write .buttonStyle(.automatic) instead of naming the type. The Cancel button uses this form; it resolves to the context-driven default styling exactly like DefaultButtonStyle() does, and reads more naturally in a modifier chain.

  5. Let context and role shape the result

    Because the default style defers to its environment, the same style declaration renders differently depending on where the button lives and what role it carries. The Cancel button's role: .cancel informs that contextual treatment, demonstrating that DefaultButtonStyle adapts rather than imposing a fixed look.

Try it — Move the two buttons inside an .alert(...) presentation and observe how the same DefaultButtonStyle() and .automatic declarations switch to the bordered, prominent appearance the system uses for buttons in that context.

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.

DefaultButtonStyle.swift
struct DefaultButtonStyleDemo: View {
    var body: some View {
        VStack(spacing: 16) {
            Button("Save Changes") {}
                .buttonStyle(DefaultButtonStyle())

            Button("Cancel", role: .cancel) {}
                .buttonStyle(.automatic)
        }
        .padding()
    }
}
Live preview
Save Changes Cancel
Save Changes Cancel
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →