
- SwiftUI - Home
- SwiftUI - Overview
- SwiftUI vs UIkit
- SwiftUI Environment
- SwiftUI - Environment Setup
- SwiftUI - Basic Components
- SwiftUI - Building First Application
- SwiftUI Views
- SwiftUI - Views
- SwiftUI - Customize Text View
- SwiftUI - Custom Image View
- SwiftUI - Stacks
- SwiftUI Drawing Shapes
- SwiftUI - Shapes
- SwiftUI - Drawing line
- SwiftUI - Drawing Rectangle
- SwiftUI - Drawing Rounded Rectangle
- SwiftUI - Drawing Triangle
- SwiftUI - Drawing Circle
- SwiftUI - Drawing Star
- SwiftUI - Drawing Polygon
- SwiftUI - Drawing Pie chart
- SwiftUI - Using built-in shapes
- SwiftUI - Text
- SwiftUI - Text View
- SwiftUI - Text Input and Output
- SwiftUI - Color
- SwiftUI - Color
- SwiftUI - Colorpicker
- SwiftUI - Gradients
- SwiftUI - Adjust Color
- SwiftUI - Effects
- SwiftUI - Effects
- SwiftUI - Blend Effect
- SwiftUI - BLur Effect
- SwiftUI - Shadow Effect
- SwiftUI - Hover Effect
- SwiftUI - Animations
- SwiftUI - Animations
- SwiftUI - Creating Animations
- SwiftUI - Creating an Explicit Animation
- SwiftUI - Multiple Animations
- SwiftUI - Transitions
- SwiftUI - Asymmetric Transition
- SwiftUI - Custom Transition
- SwiftUI - Image
- SwiftUI - Images
- SwiftUI - Image as Background
- SwiftUI - Rotating Image
- SwiftUI - Media
- SwiftUI - View Layout
- SwiftUI - View Layout
- SwiftUI - View Size
- SwiftUI - View Spacing
- SwiftUI - View Padding
- SwiftUI - UI Controls
- SwiftUI - UI Controls
- SwiftUI - Button
- SwiftUI - CheckBox
- SwiftUI - Menubar
- SwiftUI - Toolbar
- SwiftUI - Search Bar
- SwiftUI - TextField
- SwiftUI - Slider
- SwiftUI - Toggle
- SwiftUI - Pickers
- SwiftUI - Menus
- SwiftUI - List & Tables
- SwiftUI - Lists
- SwiftUI - Static List
- SwiftUI - Dynamic List
- SwiftUI - Customize List
- SwiftUI - Tables
- SwiftUI - Forms
- SwiftUI - Forms
- SwiftUI - Breaking Forms in Sections
- SwiftUI - Event Handling
- SwiftUI - Event Handling
- SwiftUI - Gesture
- SwiftUI - Clipboard
- SwiftUI - Drag and Drop
- SwiftUI - Focus
- SwiftUI - Alert
- SwiftUI - Miscellaneous
- SwiftUI - Containers
- SwiftUI - Navigation
- SwiftUI - Notifications
- SwiftUI - Cross-Platform UI
- SwiftUI - Data
- SwiftUI - Accessibility
- SwiftUI - Framework Integration
- SwiftUI - Framework Integration
- SwiftUI - Interfacing with UIKit
- SwiftUI - Creating macOS App
- SwiftUI Useful Resources
- SwiftUI - Useful Resources
- SwiftUI - Discussion
SwiftUI-View Padding
The padding adds extra space around the UI elements to make them more readable. It is the most commonly used tool in user interface design. It improves both the functionality and appearance of the interface. It prevents content from appearing cluttered and ensures a clear distinction between each UI component on the view.
The ".padding" Modifier
SwiftUI supports a built-in modifier named padding() for padding. This modifier adds extra space around the content of a view. This extra space separates the content and the edges of the view which makes the view more attractive and readable. We can apply padding uniformly to all sides of the view or the individual edges.
This modifier can work with or without parameters. If this modifier does not contain any parameter, it will apply the same amount of padding to all the edges of the view.
Syntax
Following is the syntax −
.padding(edges, amount)
Parameter
This method can have the following optional parameters −
edge: It is used to specify on which side the padding should applied. The default value of this parameter is .all. It supports the following options −
.all: It applies the same padding to all the edges of the view.
.top: It applies padding only to the top edge of the view.
.bottom: It applies padding only to the bottom edge of the view.
.trailing: It applies padding only to the right edge of the view.
.leading: It applies padding only to the left edge of the view.
.horizontal: It applies padding to the top and bottom edges of the view.
.vertical: It applies padding to the left and right edges of the view.
amount: It represents the amount of padding to be applied to the edges. The default value of this parameter is nil.
Let's understand the padding with the help of the following examples −
Example 1
The following SwiftUI program is used for uniform padding. Uniform padding is used when we want to apply the same padding to all the sides of a view. For the uniform padding, we will use the padding() modifier without any parameters.
import SwiftUI struct ContentView: View { var body: some View { VStack{ // Without padding Rectangle() .fill(Color.yellow) .frame(width: 300,height: 100, alignment: .center) .overlay(Text("TutorialsPoint").bold()) // With padding Rectangle() .fill(Color.yellow) .frame(width: 300,height: 100, alignment: .center) .overlay(Text("TutorialsPoint").bold().padding()) } } } #Preview { ContentView() }
Output

Example 2
The Following SwiftUI program is used for custom padding value. Custom padding is used when we want to apply a specific amount of padding uniformly to all the sides of the view. Here we pass a value to the padding(30) modifier for custom padding.
import SwiftUI struct ContentView: View { var body: some View { VStack{ // Without padding Rectangle() .fill(Color.yellow) .frame(width: 300,height: 100, alignment: .center) .overlay(Text("TutorialsPoint").bold()) // With custom padding Rectangle() .fill(Color.mint) .frame(width: 300,height: 100, alignment: .center) .overlay(Text("TutorialsPoint").bold().padding(100)) } } } #Preview { ContentView() }
Output

Example 3
The Following SwiftUI program is used for padding values on specific sides. Here we apply padding on some specific side by passing the sides and their padding length in the padding modifier.
import SwiftUI struct ContentView: View { var body: some View { VStack{ // Without padding Rectangle() .fill(Color.yellow) .frame(width: 300,height: 100, alignment: .center) .overlay(Text("TutorialsPoint").bold()) // With padding Rectangle() .fill(Color.mint) .frame(width: 300,height: 100, alignment: .center) .overlay(Text("TutorialsPoint").bold().padding(.bottom, 50).padding(.leading, 40)) } } } #Preview { ContentView() }
Output

Example 4
The Following SwiftUI program is used for padding with multiple sides. Here we apply padding on multiple sides at once by passing an array in the .padding() modifier.
import SwiftUI struct ContentView: View { var body: some View { VStack{ // Without padding Rectangle() .fill(Color.yellow) .frame(width: 300,height: 100, alignment: .center) .overlay(Text("TutorialsPoint").bold()) // With padding Rectangle() .fill(Color.mint) .frame(width: 300,height: 100, alignment: .center) .overlay(Text("TutorialsPoint").bold().padding([.top,.leading], 50)) } } } #Preview { ContentView() }
Output
