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

Is there a way to conditionally set the default padding to one specific edge?

I want to use the default padding on the trailing edge but only if a condition is true.

It is important for me to use the default padding and not a value so SwiftUI can set it for me.

This is my current setup using a ternary operator:

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

MyView()
    .padding(condition ? .trailing : /* what here?*/)

using nil or .none do not work and will give me an error

Any Ideas?

>Solution :

The documentation for padding’s length parameter says: “If you set the value to nil, SwiftUI uses a platform-specific default amount.”

So you can always specify a trailing padding but set the length to nil to get the default and to 0 for no padding:

MyView()
    .padding(.trailing, condition ? nil : 0)
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