What is the default value of the padding modifier in swift?

Viewed 6037

just a quick question. I couldn't find the default value of Swift's .padding() modifier.

ModelSelectorItem(variant: variant)
    .padding()

I know that I can just omit the value and swift is providing the default value on its own.

Q: What is the default value of the .padding() modifier in SwiftUI?

3 Answers

As far as I understood from Apple's documentation, there's no standard value and it's calculated based on some criteria by Apple. So, it may be different for different devices, accessibility settings of user, if user is using the app in side-by-side mode on iPad, etc...

Here is the documentation:

The set of edges along which to pad this view; if nil the specified or system-calculated amount is applied to all edges.

I created this test and you can see the result below the code:

import SwiftUI

struct Test: View {
    var body: some View {
        VStack{
            Text("Hello, World!")
                .padding()
                .background(Color.red)
            
            Text("Hello, World!")
                .padding(16)
                .background(Color.blue)
        }
        
    }
}

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        Test()
    }
}

As stated in the documents, there's no standard value for padding() and it could be different on different platforms.

What I suggest you, to create your own modifier with the following code:

struct MyDefaultPaddingModifier: ViewModifier {    
    func body(content: Content) -> some View {
        return content
            .padding(.all, 5) // you can store this as a variable based on your needs
    }
}

Then you have elegant usage:

ModelSelectorItem(variant: variant)
    .modifier(MyDefaultPaddingModifier())
Related