A button plays a vital role in an application. It has an action that results in a different path for the application user, changes the application state, and so on.
Different button goals usually have different button styles. For example, if the application prefers the user to tap a button rather than ignore it, the button should be highlighted with a primary color. If the application prefers the user to avoid a button, it should be colored red.
In this tutorial, you will learn how to style a button so you can apply your use case to your application.
How Many Built-in Button Styles are in SwiftUI?
As of Xcode 14, there are seven built-in button styles that you can use on iOS, macOS, or tvOS.
These button styles are not mutually exclusive, which means you can use them on all platforms, or some button styles are just available on one platform.
The button styles are:
automatic
button style
bordered
button style
borderedProminent
button style
borderless
button style
card
button style
link
button style
plain
button style
These button styles are available to use by passing a static variable to the
.buttonStyle
modifier in your SwiftUI button.Button("My Button") { print("You tap my button") }.buttonStyle(.card)
What is the Default Button Style?
The default button style is
.automatic
. So if you write simple code like below, it uses .automatic
button style automaticallyButton("Hello World") { }
By automatically, it means SwiftUI will choose the appropriate button style for the respective platform.
Below is the screenshoot if you use the default button style in iOS:
Below is the screenshoot if you use the default button style in macOS:
A Special Invitation for Mozzlog Reader
Thanks for Stopping By! đ€
There are many side projects that Mozzlog team is currently working on. Go and check if one of them benefits your daily development activities. đ
Or you can Join The Waitlist to get seat for upcoming premium features*. đ€©
See the projects today! https://mozzlog.com/projects đ
How to Change Button Style?
You can use
.buttonStyle
modifier that is available in all View. If you use this modifier to specific button style instance, it will only be applied to that instance.In this code you will use the
.bordered
button style for our Login button.Button("Login") { } .buttonStyle(.bordered)
If you use
.buttonStyle
on a container, the button style will be applied to every Button
childrenVStack { Button("Bordered Button 1") { } Button("Bordered Button 2") { } } .buttonStyle(.bordered)
Take a Look to The Built-in Button Styles
.automatic
button style example
The default button style, based on the buttonâs context. When you donât apply any button style, this is the default to use.
.bordered
button style
A button style that applies standard border artwork based on the buttonâs context.
.borderedProminent
button style example
A button style that applies standard border prominent artwork based on the buttonâs context.
.borderless
button style example
A button style that doesnât apply a border. This is also default button style when you use
.automatic
.card
button style example (tvOS)
A button style that doesnât pad the content, and applies a motion effect when a button has focus.
This button style available on tvOS
.link
button style example (macOS)
A button style for buttons that emulate links.
This button style available on macOS
.plain
button style example
A button style that doesnât style or decorate its content while idle, but may apply a visual effect to indicate the pressed, focused, or enabled state of the button.
Conclusion
Using custom border style for a button is super easy in SwiftUI. You can apply the style to a specific button or to multiple button at once. Using
.automatic
will use the .borderless
style, the other style allows you to style the button design with much more freedom.