SwiftUI .padding()
Published Feb 5, 2023
Contribute to Docs
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:
edgespecifies 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 .topTop edge of the view .bottomBottom edge of the view .trailingRight edge of the view .leadingLeft edge of the view .horizontalTop and bottom edges of the view .verticalLeft and right edges of the view The
amountof padding is given in points as an integer. If given anilvalue, 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:

Contribute to Docs
- 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.
Learn SwiftUI on Codecademy
- Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
- Includes 7 Courses
- With Certificate
- Beginner Friendly.13 hours
- A powerful programming language developed by Apple for iOS, macOS, and more.
- Beginner Friendly.12 hours