Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading