5

I'm trying to make a SwiftUI button with .buttonStyle(.bordered) have the full width of the VStack it's in. Here's my button code:

Button("Save", action:saveUser)
    .frame(maxWidth:.infinity)
    .buttonStyle(.borderedProminent)

In the preview, I can see the actual frame is the full width of its container, but the background provided by the buttonStyle is not:

Button background is not full width

How can I make the border provided the button style also be full width?

1
  • Before applying any style set the frame. In my case i was using custom button style so before applying style, i used frame modifier and it worked. Commented Aug 29, 2024 at 15:00

1 Answer 1

20

Use this way to make a full-width button.

Button(action: {
    saveUser()
}) {
    Text("Save").frame(minWidth: 0, maxWidth: .infinity)
}.buttonStyle(.borderedProminent)
Sign up to request clarification or add additional context in comments.

1 Comment

Is a minWidth necessary? Also seems to work without setting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.