SwiftUI Button: Styles, Roles & Custom ButtonStyle
Everything you need to build production buttons in SwiftUI — from the built-in .bordered styles to fully custom ButtonStyle types, async actions, and iOS 26 Liquid Glass.
A SwiftUI Button pairs an action closure with a label view: Button("Save") { save() }. Apply visual treatment with .buttonStyle(.bordered), .borderedProminent, or .plain, and communicate intent with .buttonStyle roles like Button(role: .destructive). For fully custom looks, conform a type to the ButtonStyle protocol and read configuration.isPressed. On iOS 26, .buttonStyle(.glass) and the .glassEffect() modifier render Apple's Liquid Glass material.
Variants: Primary, Secondary, Ghost, Destructive, Liquid Glass
Install the free SwiftUI button
- Add SwiftKitUI.swift to your app once. It holds the tokens and motion curves.
- Copy SKButton.swift below into your project. It needs nothing else.
Usage
Button("Continue") { }.buttonStyle(.sk)
Button("Delete") { }.buttonStyle(.sk(.destructive))
Button("Buy") { }.buttonStyle(.sk(.primary, loading: isLoading))Full source · 170 lines · MIT
//
// SKButton.swift
// Swift Kit UI
//
// Requires: SwiftKitUI.swift
//
// Five button roles on one interruptible press animation. The press uses a
// spring rather than a duration curve so a rapid double-tap does not queue up
// two animations and lag behind the finger — the second press retargets the
// first mid-flight.
//
// Usage:
// Button("Continue") { }.buttonStyle(.sk)
// Button("Delete") { }.buttonStyle(.sk(.destructive))
// Button("Buy") { }.buttonStyle(.sk(.primary, loading: isLoading))
//
import SwiftUI
public struct SKButtonStyle: ButtonStyle {
public enum Role: Sendable {
case primary
case secondary
case ghost
case destructive
case liquidGlass
}
private let role: Role
private let fullWidth: Bool
private let loading: Bool
public init(_ role: Role = .primary, fullWidth: Bool = true, loading: Bool = false) {
self.role = role
self.fullWidth = fullWidth
self.loading = loading
}
public func makeBody(configuration: Configuration) -> some View {
SKButtonBody(
configuration: configuration,
role: role,
fullWidth: fullWidth,
loading: loading
)
}
}
private struct SKButtonBody: View {
let configuration: ButtonStyleConfiguration
let role: SKButtonStyle.Role
let fullWidth: Bool
let loading: Bool
@Environment(\.isEnabled) private var isEnabled
private var isInteractive: Bool { isEnabled && !loading }
var body: some View {
ZStack {
// The label stays in the layout while loading so the button does not
// resize when it starts — a resizing button mid-tap feels broken.
label
.opacity(loading ? 0 : 1)
if loading {
ProgressView()
.progressViewStyle(.circular)
.tint(foreground)
.transition(.opacity.combined(with: .scale(scale: 0.85)))
}
}
.frame(maxWidth: fullWidth ? .infinity : nil)
.padding(.vertical, SK.Space.md + 2)
.padding(.horizontal, SK.Space.xl)
.background { background }
.clipShape(RoundedRectangle(cornerRadius: SK.Radius.md, style: .continuous))
.overlay { border }
.compositingGroup()
.scaleEffect(configuration.isPressed && isInteractive ? 0.97 : 1)
.opacity(isEnabled ? 1 : 0.45)
.skAnimation(.micro, value: configuration.isPressed)
.skAnimation(.snap, value: loading)
.contentShape(.rect)
.onChange(of: configuration.isPressed) { _, pressed in
// Fire on press-down, not on release. Feedback that arrives after
// the finger lifts reads as lag.
if pressed && isInteractive { SK.Haptic.play(.light) }
}
.accessibilityAddTraits(.isButton)
// Announce the busy state as a VALUE, not a label — overriding the
// label here would erase the button's own title for VoiceOver users.
.accessibilityValue(loading ? "Loading" : "")
}
private var label: some View {
configuration.label
.font(SK.Type_.headline)
.foregroundStyle(foreground)
}
@ViewBuilder
private var background: some View {
switch role {
case .primary:
SK.Palette.accent
case .secondary:
SK.Palette.fill
case .ghost:
Color.clear
case .destructive:
SK.Palette.danger
case .liquidGlass:
Color.clear.skSurface(.liquidGlass, radius: SK.Radius.md)
}
}
@ViewBuilder
private var border: some View {
let shape = RoundedRectangle(cornerRadius: SK.Radius.md, style: .continuous)
switch role {
case .secondary:
shape.strokeBorder(SK.Palette.separator, lineWidth: SK.Stroke.thin)
case .ghost:
shape.strokeBorder(SK.Palette.separator, lineWidth: SK.Stroke.thin)
default:
EmptyView()
}
}
private var foreground: Color {
switch role {
case .primary, .destructive: .white
case .secondary, .ghost, .liquidGlass: SK.Palette.label
}
}
}
extension ButtonStyle where Self == SKButtonStyle {
/// `Button(...).buttonStyle(.sk)`
public static var sk: SKButtonStyle { SKButtonStyle() }
/// `Button(...).buttonStyle(.sk(.secondary))`
public static func sk(
_ role: SKButtonStyle.Role,
fullWidth: Bool = true,
loading: Bool = false
) -> SKButtonStyle {
SKButtonStyle(role, fullWidth: fullWidth, loading: loading)
}
}
#Preview("SKButton") {
@Previewable @State var loading = false
VStack(spacing: SK.Space.md) {
Button("Primary") { }.buttonStyle(.sk)
Button("Secondary") { }.buttonStyle(.sk(.secondary))
Button("Ghost") { }.buttonStyle(.sk(.ghost))
Button("Destructive") { }.buttonStyle(.sk(.destructive))
Button("Liquid Glass") { }.buttonStyle(.sk(.liquidGlass))
Button("Disabled") { }.buttonStyle(.sk).disabled(true)
Button("Tap to load") { loading.toggle() }
.buttonStyle(.sk(.primary, loading: loading))
}
.padding(SK.Space.xl)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(SK.Palette.surfaceSunken)
}
Roles: destructive and cancel
A button role tells SwiftUI the semantic meaning of the action so it can style and place it correctly. Button(role: .destructive) renders red inside menus and confirmation dialogs, and .cancel gets special treatment in dialogs and alerts. Roles are not just cosmetic — VoiceOver announces them, and confirmationDialog uses them to order and emphasize choices. Use them instead of hardcoding a red tint on a delete button.
struct DeleteRow: View {
@State private var confirming = false
var body: some View {
Button("Delete Account", role: .destructive) {
confirming = true
}
.confirmationDialog("Delete this account?",
isPresented: $confirming,
titleVisibility: .visible) {
Button("Delete", role: .destructive) { deleteAccount() }
Button("Cancel", role: .cancel) { }
}
}
}Async actions, disabled state, and iOS 26 Liquid Glass
A button action is synchronous, but you often want to await work. Wrap the call in a Task and drive a loading flag so you can disable the button and show a ProgressView while it runs. Use .disabled(_:) to gate the button on validation, and it will dim and stop responding automatically. On iOS 26, Apple introduced Liquid Glass — apply .buttonStyle(.glass) for a system glass button, or wrap custom content in .glassEffect() to float it above scrolling content. Gate glass code behind an availability check so iOS 16–25 users fall back gracefully.
struct SubmitButton: View {
@State private var isLoading = false
let isValid: Bool
var body: some View {
Button {
Task {
isLoading = true
defer { isLoading = false }
try? await submit()
}
} label: {
if isLoading { ProgressView() } else { Text("Submit") }
}
.disabled(!isValid || isLoading)
.modifier(GlassIfAvailable())
}
}
struct GlassIfAvailable: ViewModifier {
func body(content: Content) -> some View {
if #available(iOS 26, *) {
content.buttonStyle(.glass)
} else {
content.buttonStyle(.borderedProminent)
}
}
}Ship your SwiftUI app in 5 emails
A free 5-part course on the parts that actually stall launches: paywall, auth, onboarding, App Store review, and pricing. No fluff, unsubscribe anytime.
Stop rebuilding the same button
The Swift Kit ships a tokenized PrimaryButton and pressable ButtonStyle wired into a light/dark design system, so your CTAs look consistent from day one.
Rather have it done for you? I set up and ship your app from $499. See how
How to build a custom primary button in SwiftUI
Create a reusable, on-brand primary button you can drop anywhere in your app.
- 1
Define the ButtonStyle
Create a struct conforming to ButtonStyle and implement makeBody(configuration:).
struct PrimaryButtonStyle: ButtonStyle { /* ... */ } - 2
Read isPressed
Use configuration.isPressed to add a scale or opacity change for tactile feedback.
.scaleEffect(configuration.isPressed ? 0.97 : 1) - 3
Apply and reuse
Attach it with .buttonStyle(PrimaryButtonStyle()) on any Button in the app.
Button("Save") { save() }.buttonStyle(PrimaryButtonStyle())
Frequently Asked Questions
How do I make a filled primary button in SwiftUI?
How do I add an async action to a SwiftUI Button?
How do I make a destructive delete button in SwiftUI?
How do I detect the pressed state of a SwiftUI Button?
How do I make an iOS 26 Liquid Glass button in SwiftUI?
Keep exploring
Ship faster with a real design system
The Swift Kit is a $99 one-time SwiftUI boilerplate with buttons, paywalls, auth, and AI already wired up — lifetime updates and a 14-day refund.
Get The Swift Kit — $99One-time purchase · Lifetime updates · 14-day refund