
- 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- Navigation
SwiftUI Navigation refers to the tools and methods available in SwiftUI to manage navigation between different views within an app. It allows developers to create hierarchical interfaces where users can move from one screen (view) to another with ease, often utilizing navigation bars, back buttons, and a stack-based system.
SwiftUI offers several components to facilitate navigation in a declarative way.
Key Components of Navigation
Following are the major components that are commonly used in SwiftUI:
NavigationView
NavigationLink
NavigationStack
NavigationBar
NavigationView
NavigationView is a container view in SwiftUI that allows you to create a navigation-based interface, where users can move from one screen (view) to another. It automatically provides the functionality for a navigation bar, where you can display titles, buttons, and other controls, and it also facilitates hierarchical navigation.
Example
The following SwiftUI program is used for navigationView.
import SwiftUI struct ContentView: View { var body: some View { // Wrapping the content in a NavigationView NavigationView { VStack { Text("Home Screen").font(.largeTitle).padding() // NavigationLink to navigate to the next screen NavigationLink(destination: DetailView()) { Text("Go to Detail Screen") .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(10) }.padding() }.navigationTitle("Home") } } }
Output

NavigationLink
NavigationLink is a crucial element in SwiftUI that enables navigation between views within a NavigationView (or NavigationStack in iOS 16+). It creates a tappable area in your interface that, when activated, triggers a transition to another view.
Example
The following SwiftUI program is used for navigationLink.
import SwiftUI struct ContentView: View { var body: some View { NavigationView { VStack { Text("Home Screen").font(.largeTitle).padding() // NavigationLink to navigate to the next screen NavigationLink(destination: DetailView()) { Text("Go to Detail Screen") .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(10) }.padding() }.navigationTitle("Home") } } }
Output

NavigationStack
NavigationStack is a new navigation container introduced in iOS 16 to replace NavigationView for managing navigation in a more flexible and efficient way. It provides a more modern and powerful approach to handle navigation, especially when dealing with dynamic data or complex navigation patterns.
Example
The following SwiftUI program is used for navigationStack.
import SwiftUI struct ContentView: View { var body: some View { NavigationStack { VStack { Text("Home Screen") .font(.largeTitle) .padding() // NavigationLink to navigate to the next screen NavigationLink("Go to Detail Screen", value: "Hello from Home Screen") .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(10) }.navigationTitle("Home") // Set the navigation title for the Home screen } } }
Output

NavigationBar
The navigation bar in SwiftUI refers to the bar that typically appears at the top of the screen in a NavigationView or NavigationStack, where you can display the title of the current screen and add buttons or actions.
This is a key UI component in navigation-based apps that helps users understand their location in the app and provides controls for navigation and actions (like going back, saving, etc.).
Example
The following SwiftUI program is used for navigationBar.
import SwiftUI struct ContentView: View { var body: some View { NavigationView { VStack { Text("Welcome to the Home Screen") .font(.largeTitle) .padding() NavigationLink("Go to Detail Screen", destination: DetailView()) .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(10) }.navigationTitle("Home") // Set the title of the navigation bar } } } struct DetailView: View { var body: some View { VStack { Text("Detail Screen") .font(.largeTitle) .padding() }.navigationTitle("Detail") // Set a different title for the Detail screen } } @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
Output
