Chapters

Hide chapters

SwiftUI by Tutorials

Fourth Edition · iOS 15, macOS 12 · Swift 5.5 · Xcode 13.1

Before You Begin

Section 0: 4 chapters
Show chapters Hide chapters

13. Navigation
Written by Bill Morefield

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

It’s rare to find an app that can work with only a single view; most apps use many views and provide a way for the user to navigate between them smoothly. The navigation you design has to balance many needs: You need to display data logically to the user, you need to provide a consistent way to move between views, and you need to make it easy for the user to figure out how to perform a particular task.

SwiftUI provides a unified interface to manage navigation while also displaying data. In this chapter, you’ll explore building a navigation structure for an app.

Getting started

Open the starter project for this chapter; you’ll find a very early version of a flight-data app for an airport. In this chapter, you will build out the navigation for this app. In a real-world app, you would likely get the flight information from an API through Combine. For this app, though, you’ll be using mock data.

To start, expand the Models folder in the app. Open FlightData.swift, and you’ll find the implementation of the mock data for this app. The FlightData class generates a schedule for fifteen days of flights with thirty flights per day starting with today’s date using the generateSchedule() method. The class uses a seeded random number generator to produce a consistent set of flight data every time with only the start date changing.

Also open and examine FlightInformation.swift, which encapsulates information about flights. You’ll be using this mock data through the next several chapters while building out this app.

Open WelcomeView.swift, and you’ll see the view includes a @StateObject named flightInfo that holds this mock data for the app.

Navigating through a SwiftUI app

When designing the navigation for your SwiftUI app, you must create a navigation pattern that helps the user move confidently through the app and intuitively perform tasks. Your users will rarely notice well-done navigation, but they won’t stand for an app that’s hard to navigate or that makes it hard to find information. SwiftUI is a cross-platform framework, but it takes its primary design inspiration from iOS and iPadOS. Therefore, SwiftUI integrates patterns and design guidelines that are common on those platforms.

Ckiq gunezusuim
Jvog lesegeqeiw

Roimexhpinal huhilacuuy
Piisudsducoz nisenovuoy

Creating navigation views

Build and run the starter app. You’ll see a bare-bones implementation with a graphic and a single option to view the day’s flight status board. In this chapter, you’ll change this view to use hierarchical navigation with a NavigationView.

Iqajiuf uvq
Ecuzaul anx

Tihipisiof cucno
Gukedulies gexru

Fjefm pbsour eBaj
Bbovf lslaas aCet

Pilcahx niel ud oYay
Yubvimd weug on aCev

Polishing the links

Before moving to the child navigation views, you’ll improve the button’s look from the current plain text. Create a new SwiftUI View named WelcomeButtonView.swift. Replace the default view with the following:

Gesahifaot fumq
Xugaboheug vavj

Using navigation links

You’ll first create a view that implements the first option from the WelcomeView, providing more detailed information about today’s flight to the user.

Zyogfq xosd
Tdovzl zilm

Extending the hierarchy

Your navigation follows the flow from more general information to more specific information. Displaying a list of today’s flights from the Welcome screen makes the first step. Next, you’ll show details about a flight when the user taps a flight on the list.

Nhungn nocl vewn ivxeh
Dquzfm bomk farx eltah

Ybotcc fojeilp loek
Qhulfd lowaols giul

Adding items to the navigation bar

Creating a navigation view stack adds a navigation bar to each view. By default, the navigation bar contains only a button that links back to the previous view (for all views except the first one). Beginning in iOS 14, the user can also long-press the back button to move anywhere up the view hierarchy in a single action.

Tumgbu
Wovgro

Navigation via code

The default navigation link responds to a user’s action, turning the view into a button. When the user taps that button, the movement to the next view triggers. You can also trigger this navigation by code, useful for reacting to external events or signals. To do so, you use a variation of the NavigationLink methods you’ve created to this point in the chapter.

Buwdc pvesgg on lzi doj
Cetvm xfixfp an qka quq

Sharing the environment

As you saw earlier, it’s simple to pass data down the navigation stack. You can send the data as a read-only variable or pass a binding to allow the child view to make changes reflected in the parent view. That works well for direct cases, but as the view hierarchy’s size and complexity increase, you’ll find that sending information back up can get complicated.

Jofahomoub wiupjud
Zomaqiluur taukcif

Bugafgod cpoqgh az Wozxafu hear
Ciloqxod nfegbm uv Yuwxoru raed

Using tabbed navigation

You’ve been using and building a hierarchical view stack with NavigationView to this point in the app. Most apps use this structure, but there is an alternative structure built around tabs. Tabs work well for content where the user wants to flip between options. In this app, you’ll implement tabs to show different versions of the flight status view.

Dficvy vhevoy dipc cogh
Pvedxt ljeyex zipr peyv

Setting tabs

It would be a nice addition to remember the last tab selected when the user returns to the view. Still in FlightStatusBoard.swift, below the hidePast state variable add the following line:

Setting tab badges

SwiftUI 3 introduced controls that let you set a badge for each tab. This badge provides extra information to the user, but the available space limits the amount of data you can show. You’ll add a badge item to show the number of flights for incoming and outgoing flights to the Flight Status along with a short text badge showing the date.

Wettas
Nejhot

Key points

  • App navigation generally combines a mix of flat and hierarchical flows between views.
  • Tab views display flat navigation that allows quick switching between the views.
  • Navigation views create a hierarchy of views as a view stack. The user can move further into the stack and can back up from within the stack.
  • A navigation link connects a view to the next view in the view stack.
  • You should only have one NavigationView in a view stack. Views that follow should inherit the existing navigation view.
  • You apply changes to the navigation view stack to controls in the stack, and not to the NavigationView itself.

Where to go from here?

The first stop when looking for information on user interfaces on Apple platforms should be the Human Interface Guidelines on Navigation for iOS, watchOS and tvOS:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now