
- 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 Spacing
Spacing is the most important feature of SwiftUI, it allows user to adjust the space between the views. It enhances the appearance of views and make them much more readable. We can control the spacing using either through default values or by custom adjustment. In SwiftUI, we can manage spacing using the following methods −
spacer() method
spacing property
Spacer() Method in SwiftUI
The Spacer() method is used to create a space between the given views. It provide maximum space in compare with other spacing methods. It is commonly used in stack layout, but we are allowed to use it outside the stack layout. If it is used inside a stack layout, then it expend along the axis of its containing stack.
Suppose it is present in HStack, then it expend along the X-axis. If it is present outside the stack layout, then it expend in all the axis. We are allowed to use multiple spacer() in the same layout.
Syntax
Following is the syntax −
Spacer(minLength:CGFloat?)
It can take only one optional parameter that is minLength. It represent the minimum length to which the Spacer will shrink the space between the given views.
Example 1
The following SwiftUI program add space in between the given view.
import SwiftUI struct ContentView: View { var body: some View { VStack{ HStack{ // Without Spacer Text("Hello").font(.title2) Text("TutorialsPoint").font(.title2) } HStack{ // With Spacer Text("Hello").font(.title2) Spacer() Text("TutorialsPoint").font(.title2) } } } } #Preview { ContentView() }
Output

Example 2
The following SwiftUI program add space in between the given view.
import SwiftUI struct ContentView: View { var body: some View { VStack{ Text("Hello").font(.title2) Text("TutorialsPoint").font(.title2) } // Spacer() method outside Stack layout Spacer() VStack{ Text("Hello").font(.title2) Text("TutorialsPoint").font(.title2) } } } #Preview { ContentView() }
Output

Spacing Property in SwiftUI
We can also adjust the spacing of the view with the help of Stack layout's spacing property. This property only works within stack layout only. It can work with all three stacks including VStack, ZStack and HStack.
Syntax
Following is the syntax −
VStack(spacing:value){ //code }
Example
The following SwiftUI program add space in between the given view using spacing property.
import SwiftUI struct ContentView: View { var body: some View { // Stack without spacing parameter VStack(){ Text("Hello").font(.title2) Text("TutorialsPoint").font(.title2) } // Stack with spacing parameter VStack(spacing:40){ Text("Hello").font(.title2) Text("TutorialsPoint").font(.title2) } } } #Preview { ContentView() }
Output
