Creating Custom Button Styles in SwiftUI

Saeid Rezaeisadrabadi
3 min readApr 22, 2024

--

In this post, I want to talk about custom styling in SwiftUI. SwiftUI offers a powerful and intuitive way to build user interfaces. Buttons are a fundamental building block in any app, and customizing their appearance can significantly enhance the user experience and match your app’s unique design. This is where SwiftUI’s buttonStyle modifier comes in.

Generated with AI

The Power of ButtonStyle

Instead of applying individual modifiers like .background() and .foregroundColor() to each button, buttonStyle lets you define a reusable style that encapsulates all the desired customizations. This promotes code maintainability and consistency across your app's buttons.

Creating Your Custom ButtonStyle

To create a custom button style, we’ll leverage Swift’s powerful protocol-oriented approach. Here’s a step-by-step walkthrough:

  1. Define a new struct: This struct will represent your custom button style and conform to the ButtonStyle protocol.
  2. Implement the makeBody function: This function takes a configuration parameter of type ButtonStyleConfiguration, which provides information about the button's current state (pressed, highlighted, etc.) and its label. It's your playground to define how the button should look and behave based on this configuration.

Here’s an example of a basic custom button style that creates a rounded capsule-shaped button with white text:

struct CapsuleButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding()
.foregroundColor(.white)
.background(RoundedRectangle(cornerRadius: 10).fill(.blue))
.clipShape(Capsule())
}
}

How to use the ButtonStyle

Once you have your custom style defined, applying it to a button is straightforward. Simply use the buttonStyle modifier:

Button("Press Me") {
// Button action
}
.buttonStyle(CapsuleButtonStyle())

Beyond the Basics

The true potential of buttonStyle shines when you incorporate dynamic styling based on the button's state. You can access the configuration.isPressed property to change the button's appearance when pressed, creating a more responsive user experience.

Taking it Further

SwiftUI provides even more control by allowing you to define custom button styles for specific button types like Toggle or Link. Explore these options to achieve a truly cohesive design for your app's interactive elements.

The other built-in views we can create custom styles for are:

  • Toggle
  • Date­Picker
  • Gauge
  • Progress­View
  • Label
  • Labeled­Content
  • Disclosure­Group
  • Control­Group
  • Group­Box
  • Form

Conclusion

Custom button styles in SwiftUI empower you to design unique and engaging buttons that elevate your app’s look and feel. By following these steps and unleashing your creativity, you can craft buttons that seamlessly integrate with your app’s overall design language.

Bonus Tip: For even more advanced customization, explore using environment values within your custom button style to control aspects like loading states or user permissions. This allows for centralized management of button behavior across your app.

With SwiftUI’s buttonStyle modifier, the possibilities for crafting unique and interactive buttons are endless. So, ditch the generic buttons and start building a custom button style library that injects personality into your SwiftUI applications!

--

--

Saeid Rezaeisadrabadi
Saeid Rezaeisadrabadi

Written by Saeid Rezaeisadrabadi

Over 8 years of experience in iOS software development

No responses yet