Logo for tanaschita.com

Customizing buttons in SwiftUI with the ButtonStyle protocol

Learn how to reuse button styles in SwiftUI.

updated on 08 Sep 2025 · 2 min read

SwiftUI provides a ButtonStyle protocol which allows us to create reusable button styles without having to create new views.

Let's directly jump into an example.

Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE

When working with buttons, SwiftUI provides modifiers to customize them. Instead of writing:

Button("Primary", action: someAction)
.padding(.horizontal, 20)
.padding(.vertical, 10)
.foregroundColor(.white)
.background(Color.red)
.clipShape(RoundedRectangle(cornerRadius: 10))

we can encapsulate that style:

struct PrimaryButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding(.horizontal, 20)
.padding(.vertical, 10)
.foregroundColor(.white)
.background(Color.red)
.clipShape(RoundedRectangle(cornerRadius: 10))
}
}

Then reuse it cleanly:

Button("Primary", action: someAction)
.buttonStyle(PrimaryButtonStyle())

The makeBody function provides a configuration that we can use to adjust the appearance. It also provides the property isPressed which indicates whether the user is currently pressing the button.

.foregroundColor(configuration.isPressed ? .brown : .red)

Another benefit we get is that we can set the button style in the environment of a view.

VStack(spacing: 10) {
Button("Primary button 1") {}
Button("Primary button 2") {}
Button("Primary button 3") {}
}
.buttonStyle(PrimaryButtonStyle())

Setting the button style on the VStack will automatically apply it to all the buttons inside the view.

Sponsorship logo
Preparing for a technical iOS job interview
Preparing for a technical iOS Job Interview with over 300 questions & answers. Covering Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, iOS File System, Core Data, Concurrency with async/await, Security, Automated Testing, Dependency Management, AI & Machine Learning and more.
LEARN MORE
Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE
Sponsorship logo
Become a sponsor of tanaschita.com
By publishing an article on different iOS topics every week, tanaschita.com is constantly growing in the developer community and may provide a great audience for you as a sponsor.
CLICK TO LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

swiftui

swift

concurrency

async/await

ios

Building responsive and reliable search in SwiftUI

Learn how to debounce search input, cancel obsolete requests and prevent stale results using Swift concurrency.

14 Sep 2026 · 6 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum