Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maxWidth infinity failure SwiftUI

I have the following code:

Button(action: {
                }, label: {
                    Text("Save".uppercased())
                        .foregroundColor(.white)
                        .font(.headline)
                        .background(Color.accentColor)
                        .frame(height: 55)
                        .frame(maxWidth: .infinity)
                        .cornerRadius(10)
                })
            }
            .padding(14)

I've checked it over and am clearly missing something because the max width is not working whatsoever. The button is still tightly confined around the "SAVE" text. I have also tried manually adjusting the width but this hasn't changed anything.

Any suggestions? I am running XCode 13.

like image 609
FlexBoxer Avatar asked Aug 06 '26 18:08

FlexBoxer


1 Answers

Order matters a lot in view modifiers ;)
I suppose you want this:

            Text("Save".uppercased())
                .frame(maxWidth: .infinity)
                .frame(height: 55)
                .background(Color.accentColor)
                .cornerRadius(10)
            
                .foregroundColor(.white)
                .font(.headline)

The Text itself is only as tall & wide as it needs to be, so first the frames to define the size, then the background color for that area, then the corner radius.
Foreground color and font can go anywhere.

You can see and check many of the (also if working) effects in the preview, where you can select single lines of code and see the resulting frame.

like image 79
ChrisR Avatar answered Aug 09 '26 13:08

ChrisR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!