Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🔍 Overview
Navigation is essential in building any multi-screen app,
while lists and forms form the backbone of data presentation and user input.
SwiftUI simplifies these interactions with elegant, declarative structures like
NavigationStack, List, and Form.
In this chapter, you’ll learn:
🚪 1. Introduction to
NavigationStack and NavigationLink
SwiftUI introduced NavigationStack in iOS 16 as a
modern replacement for NavigationView, offering enhanced control over
stack-based navigation.
✅ Basic Navigation Example
swift
struct
HomeView: View {
var body: some View {
NavigationStack {
NavigationLink("Go to
Profile", value: "profile")
.navigationDestination(for:
String.self) { value in
if value ==
"profile" {
ProfileView()
}
}
.navigationTitle("Home")
}
}
}
🧱 Navigation Elements
Table
Element |
Description |
NavigationStack |
Container for views
that supports push-style nav |
NavigationLink |
Tappable
element to navigate to another view |
navigationTitle() |
Sets title of current
screen |
navigationDestination() |
Defines where
to navigate based on value |
📚 2. Building a
Navigation Hierarchy
Let’s build a multi-screen app with three pages: Home →
Profile → Settings.
swift
struct
HomeView: View {
var body: some View {
NavigationStack {
VStack {
NavigationLink("Go to
Profile") {
ProfileView()
}
}
.navigationTitle("Home")
}
}
}
struct
ProfileView: View {
var body: some View {
VStack {
Text("User Profile")
NavigationLink("Settings") {
SettingsView()
}
}
.navigationTitle("Profile")
}
}
struct
SettingsView: View {
var body: some View {
Text("App Settings")
.navigationTitle("Settings")
}
}
📝 3. Displaying Data with
Lists
SwiftUI’s List makes it easy to build scrollable lists,
including dynamic ones.
✅ Static List
swift
List
{
Text("Apple")
Text("Banana")
Text("Orange")
}
✅ Dynamic List
swift
let
fruits = ["Apple", "Banana", "Orange"]
List(fruits,
id: \.self) { fruit in
Text(fruit)
}
✅ Custom List Row
swift
struct
Contact: Identifiable {
let id = UUID()
let name: String
let phone: String
}
let
contacts = [
Contact(name: "Alice", phone:
"123456"),
Contact(name: "Bob", phone:
"654321")
]
List(contacts)
{ contact in
HStack {
VStack(alignment: .leading) {
Text(contact.name).font(.headline)
Text(contact.phone).font(.subheadline)
}
}
}
🔁 4. List Interactions:
Delete, Reorder, Swipe
✅ Swipe to Delete
swift
@State
private var items = ["Item 1", "Item 2", "Item
3"]
List
{
ForEach(items, id: \.self) { item in
Text(item)
}
.onDelete { indexSet in
items.remove(atOffsets: indexSet)
}
}
✅ Reorder Items
swift
List
{
ForEach(items, id: \.self) { item in
Text(item)
}
.onMove { indices, newOffset in
items.move(fromOffsets: indices,
toOffset: newOffset)
}
}
📋 5. Gathering Input with
Forms
SwiftUI's Form view is ideal for structured input, settings,
and data collection.
✅ Basic Form Example
swift
@State
private var name = ""
@State
private var notificationsEnabled = true
Form
{
Section(header: Text("Profile"))
{
TextField("Name", text:
$name)
}
Section(header:
Text("Preferences")) {
Toggle("Enable
Notifications", isOn: $notificationsEnabled)
}
}
🧾 Common Input Controls
Table
Control |
Purpose |
TextField |
Accept text input |
Toggle |
On/off switch |
Stepper |
Increment/decrement
numeric values |
Picker |
Dropdown
selection |
DatePicker |
Date/time selection |
✅ Example: Registration Form
swift
@State
private var email = ""
@State
private var password = ""
@State
private var gender = "Male"
let
genders = ["Male", "Female", "Other"]
Form
{
Section(header: Text("Login")) {
TextField("Email", text:
$email)
SecureField("Password", text:
$password)
}
Section(header: Text("Personal
Info")) {
Picker("Gender", selection:
$gender) {
ForEach(genders, id: \.self) {
gender in
Text(gender)
}
}
}
}
🧠 6. Advanced Navigation
with Data Passing
Use value-based navigation to pass data between screens.
✅ Navigation with Parameter
swift
struct
ItemView: View {
var itemName: String
var body: some View {
Text("Item: \(itemName)")
}
}
NavigationLink("Show
Item", value: "Book")
.navigationDestination(for:
String.self) { item in
ItemView(itemName: item)
}
📌 Best Practices
📊 Feature Summary Table
Component |
Purpose |
Notes |
NavigationStack |
Replaces
NavigationView |
Supports stack-based
navigation |
NavigationLink |
Navigates to
destination view |
Use with
value or destination block |
List |
Renders scrollable
table of items |
Supports dynamic and
static content |
Form |
Organizes
structured input fields |
Good for
settings, onboarding |
.onDelete |
Allows swipe-to-delete
behavior |
Requires ForEach |
.onMove |
Allows
drag-to-reorder |
Requires
EditButton |
📌 Conclusion
Navigation, lists, and forms are essential for creating
intuitive and functional SwiftUI apps. SwiftUI’s declarative approach makes
building these features faster, more readable, and more scalable.
With the ability to create dynamic lists, handle user input
with ease, and navigate across multiple screens seamlessly, you’re now equipped
to build real-world app interfaces.
In the next chapter, we’ll dive into animations,
gestures, and user interactions—bringing your UIs to life.
Answer:
SwiftUI is Apple’s declarative framework introduced in 2019 for building user
interfaces across all Apple platforms. Unlike UIKit, which is imperative and
relies on code-heavy view controllers, SwiftUI lets you describe your UI
using simple, state-driven structures. It handles layout, state updates,
and transitions more efficiently.
Answer:
Absolutely. As of 2025, SwiftUI has matured significantly with support for
complex views, navigation, animations, and interoperability with UIKit. Many
apps on the App Store are now built entirely using SwiftUI or a hybrid
approach.
Answer:
SwiftUI is supported on iOS 13 and above, but many features (like
NavigationStack, Grid, etc.) require iOS 15+ or iOS 16+. It's
recommended to target iOS 15 or higher to take full advantage of SwiftUI’s
modern APIs.
Answer:
Not necessarily. SwiftUI is self-contained and beginner-friendly. However,
understanding UIKit can be helpful when working on projects that require legacy
integration or using UIKit components via UIViewRepresentable.
Answer:
MVVM (Model-View-ViewModel) is the most natural fit for SwiftUI.
SwiftUI’s data-driven nature aligns well with observable models, helping you
separate UI from business logic efficiently.
Answer:
Yes! SwiftUI is designed to work across iOS, macOS, watchOS, and tvOS
with a shared codebase. You can create adaptive layouts and reuse components
easily between platforms.
Answer:
SwiftUI provides built-in animation support using simple modifiers like
.animation(), .transition(), and .withAnimation {} blocks. It supports both
implicit and explicit animations with customizable curves.
Answer:
Answer:
Yes! SwiftUI integrates seamlessly with Core Data using @FetchRequest
and works beautifully with Combine for reactive programming. These
integrations make building data-driven apps much easier.
Answer:
Xcode provides a live preview canvas for SwiftUI. Just use the
PreviewProvider protocol in your view:
struct
MyView_Previews: PreviewProvider {
static var previews: some View {
MyView()
}
}
This lets you see real-time changes without compiling or
running on a simulator.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)