12

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?

colin
  • 761
  • 1
  • 11
  • 24
  • 4
    I would not rely on this, it might be (and rather is) different on different platforms, and might be changed (and rather will be) in future versions. – Asperi Dec 19 '20 at 09:37
  • good point. is there a way to store the value in a variable and use that, instead of the hard-coded value? – colin Dec 19 '20 at 09:47

3 Answers3

12

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.

emreoktem
  • 2,409
  • 20
  • 36
  • 3
    Would be nice to have programatic access to this "default" value. Especially for being able to apply the negative of the default. I don't know why but it seems like Apple did not make it available. – InvisibleGorilla Feb 04 '23 at 12:44
4

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()
    }
}
F4RZ1N
  • 129
  • 2
  • 2
  • 2
    This only seems similar because the dynamically calculated value happens to be 16 for the type of device and accessibility settings. The value is not even same for non-Max and Max devices for example. – Harshad Kale Mar 02 '23 at 06:50
3

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())
Neil Galiaskarov
  • 5,015
  • 2
  • 27
  • 46