6

How to make SwiftUI button with buttonStyle expand to full width

 2 years ago
source link: https://sarunw.com/posts/swiftui-button-style-width/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to make SwiftUI button with buttonStyle expand to full width


Table of Contents

SwiftUI got many beautiful built-in button styles. One problem you might get is it isn't obvious how to control the size of it.

The following are the four built-in button styles.

Four built-in button styles: plain, bordered, bordered prominent, and borderless.
Four built-in button styles: plain, bordered, bordered prominent, and borderless.

If you try to make a full-width button using .frame(maxWidth: .infinity), you will see that the button styling isn't respecting the new frame and show at its ideal size.

VStack {
Button("Plain") {}
.buttonStyle(.plain)
.frame(maxWidth: .infinity)
.border(.pink)
Button("Bordered") {}
.buttonStyle(.bordered)
.frame(maxWidth: .infinity)
.border(.pink)
Button("Bordered Prominent") {}
.buttonStyle(.borderedProminent)
.frame(maxWidth: .infinity)
.border(.pink)
Button("Borderless") {}
.buttonStyle(.borderless)
.frame(maxWidth: .infinity)
.border(.pink)
}
.font(.title)

We try to expand a button to the full width with .frame(maxWidth: .infinity), but the button style doesn't follow the frame (pink border).

A button style doesn't expand with its frame.
A button style doesn't expand with its frame.

This is because the frame modifier creates a new frame around the button (as dictated by the pink border), which happens after the button style is applied.

You can learn more about frame in How to make a SwiftUI view to fill its container width and height.

You can easily support sarunw.com by checking out this sponsor.

emerge-avatar-rounded-small.png Sponsor sarunw.com and reach thousands of iOS developers.

How to change the width of a button with a button style

To change a button width, we need to apply .frame(maxWidth: .infinity) before we apply a button style.

To do that

  1. We create a button with an initializer that allows us to **modify the button's content, **init(action:label:).
  2. And put .frame(maxWidth: .infinity) there.
Button {

} label: {
Text("Bordered Prominent")
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)

Since .frame(maxWidth: .infinity) appllied before the button style, the style will respect the specify frame.

A button expands to the full width.
A button expands to the full width.

You may also like

How to make a SwiftUI view to fill its container width and height

We can use a frame modifier to make a view appear to be full width and height, but the result might not be what you expected.

SwiftUI
SwiftUI ButtonStyle

How to create a reusable button style in SwiftUI.

SwiftUI ButtonStyle
How to make a custom button style supports leading dot syntax in SwiftUI

In Swift 5.5, we can apply button style using leading dot syntax, .buttonStyle(.plain), instead of a full name of a concreate type, .buttonStyle(PlainButtonStyle()). Let's see how we can make our custom button style support this.

SwiftUI ButtonStyle

Read more article about SwiftUI, Button, ButtonStyle,

or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron

Buy me a coffee

Tweet

Share

Previous
Loop n times in Swift

There are many ways to loop in Swift. In this post, I will show you how I do when I want to loop for a specific number of times.

Next
How to use UIFont in SwiftUI Font

Learn how to convert UIKit UIFont to SwiftUI Font.

← Home


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK