.padding()
The .padding()
modifier method applies padding or spacing around a view.
Syntax
Text("This text has padding around it.")
.padding(edges, amount)
The .padding()
modifier method uses the following optional parameters:
edge
specifies which side(s) the padding should be applied to. The following options are possible:Option Applies padding to .all
(default option)All edges of the view .top
Top edge of the view .bottom
Bottom edge of the view .trailing
Right edge of the view .leading
Left edge of the view .horizontal
Top and bottom edges of the view .vertical
Left and right edges of the view The
amount
of padding is given in points as an integer. If given anil
value, SwiftUI assigns a default amount based on the device being used.
Using the .padding()
modifier method without any specified parameter results in the default behavior of the modifier: the default amount of spacing applied to all the edges of the view.
Example
var body: some View {Text("I have no padding.")Text("I have no padding.")Text("I have top and bottom padding!").padding(.horizontal, 40)Text("I have no padding.")}
This will display the following:
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.