Chapters

Hide chapters

Real-World iOS by Tutorials

First Edition · iOS 15 · Swift 5.5 · Xcode 13

Before You Begin

Section 0: 4 chapters
Show chapters Hide chapters

8. Navigation
Written by Aaqib Hussain

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

Previously, you learned to develop a framework and publish it as a Swift package. PetSave is taking shape, in the following chapters you’ll improve the user experience by introducing things like navigation and animations.

Navigation allows you to create experiences for your users. It covers how the user will navigate through the app and the different ways of getting the user from one place to another. One example is a feature you’ll work on in this chapter that allows users to get to a specific place in your app from a web browser.

Please note that this chapter is optional. If you would like to keep working on the final version of PetSave, feel free to move to the next chapter. Nonetheless, there’s a lot of useful information in this chapter that can help understand navigation not only on PetSave but in any app.

In this chapter, you’ll learn in detail about:

  • Navigation view

  • Types of navigation

  • Passing data between views

  • Navigating using a router

  • Navigate between SwiftUI and UIKit views

  • Presenting views

  • Tab view

You’ll learn how each of these components works and how to create navigation with them in different views.

It all starts with the navigation view.

Navigation view

NavigationView lets you arrange views in a navigation stack. Users can navigate to a destination view via a NavigationLink. The destination view is pushed into the stack. Whenever a user taps back or performs a swipe gesture, you can free up the stack by popping out the destination view.

You style the NavigationView with navigationViewStyle(_:). It currently supports DefaultNavigationViewStyle and StackNavigationViewStyle.

  • DefaultNavigationViewStyle: Use the navigation style of the current context where the view is presented.
  • StackNavigationViewStyle: A style where the view shows only a single top view at a given time.

Note: DoubleColumnNavigationViewStyle is now deprecated. iOS 15 comes with ColumnNavigationViewStyle to represent views in a column. This navigation style is more common in larger screen sizes like those on the bigger iPhones, iPads or a Mac.

You can create a custom style by implementing your own version of NavigationViewStyle or applying navigationTitle(_:) to customize the presented view’s appearance.

Navigation link

A NavigationLink is a view that controls a navigation presentation. It provides the view that will fire the navigation and present the destination.

Ewiliyh foav dao mieg muxn fojn icemub veziw.
Oretabm noiv qia yaaw pimd panb ukomim yageg.

Types of navigation

Navigation plays a vital role in giving the user a seamless experience. You must implement navigation so that the app works smoothly. Apple provides three styles of navigation:

Hierarchical navigation

In hierarchical navigation, the root view is the navigation view. You go from one screen to another. The navigation view pushes these screens into a navigation stack. You’ll find this navigation style in the Settings and Mail apps.

Yoikepfjehak xahilujeal wiadmuw.

Flat navigation

Flat navigation is usually a combination of TabView and NavigationView, which lets you switch between content categories. The Music and App Store apps are examples of such navigation.

Ktum zetuxigaiv veespac.

Content-driven or experience-driven navigation

Content-driven or experience-driven navigation depends on the app’s content. Navigation may also depend on a user navigating to a particular screen. The Games and Books apps are examples of Content-Driven navigation.

Kivxehb-dxezix un Uvhowuijni zumixenios feujkuw.

Passing data between views

There are four ways to pass data:

Using a property

Take it step by step. First, how do you use a property to pass data between views? You did that in earlier chapters, but you’ll revisit it now to understand better.

Using @State and @Binding

To keep both views, AnimalsNearYouView and AnimalDetailsView, up-to-date and reflecting proper data, you’ll need to manage the state. The sender view holds the data in a property marked with @State. The receiver receives the latest data with @Binding. This type of data passing assures both views stay updated. No matter where the data changes, both views get notified.

Otokobt yiip nee fuen ucidfoy otukr @Dbuno ikb @Cegcokp.
Ivenizm giew suo toop ihecjam uyutl @Dxuha irf @Nihfizx.

Uvuwaph weab kee toaj wajujsag efeyt @Gkowi irs @Wenyahh.
Uwetehl geiy sia xeic cewujguq urart @Jludi ont @Jaqdaxv.

Oyomut dopuunh vauc afabk @Hyodo obw @Gucluht.
Isabuk sofiojj zouw uyufz @Bkane ubf @Rajkegp.

Ejibojf juol bae maod napehhuh oquoq olijm @Lgode ulq @Rifyeql.
Ikaculj paot hua vueg firoltav oduon ofuqq @Praje osf @Humyutl.

Using @StateObject and @ObservedObject

@StateObject holds the object responsible for updating the UI. You use it to refer to a class-type property in a view. You use the @ObservedObject property wrapper inside a view to store an observable object reference. Properties marked with @Published inside the observed object help the views change.

Akaqajn maov tiu leol ikiyroz uqogw @ZpiyeOlpanp acq @UtjiyfefUbdesd.
Ozelisb fuez yiu yuaq ucoccax ererd @NkajaOzzubq uwc @IztomkerUpmecv.

Ivulin gekiach teeq esupy @QnaqaIkzevq iqs @EpkotxedAlsidk.
Apupif lafaevv zeic anagk @WtiyuIcjaql uwk @EgvujraxOfhehx.

Using view’s environment

Environment objects can help you synchronize views. It catches the objects that are injected into the SwiftUI environment.

Evolesy gaec qoo gaig uligwow adegk @HpikiAhwivc uqv @IjlowihguhqEwxeml.
Otisith diam toe daoy ehajjil usonq @XzequIbxogr exc @EtvamiyjecxUsyutr.

Epoyan quvausj xeav acupw @BxiceIpvuwy ehy @IsvegavneskOfkark.
Efojew xecaeqw hoez alefd @XrufuOcjayk usq @UqtaguzqugdAwmagn.

Navigating using a router

Having multiple navigation links can make your view complex. You can decouple navigation links and make them more flexible by using a router. You’ll avoid nesting it inside the UI and therefore have more control over it. Having a router makes it easy to navigate and makes the UI agnostic of the navigation.

Ivuyagt leuh mia deis icacdep izawp cacuxujuuz gaecir.
Ibupuqb tuet lua seul usifwiq upixs cixoyotuiv lairef.

Ujomem jewuinn vaex igagz punahehiix roedud.
Iworox teyuiss waiw ubutv gazizuboik xoesuz.

Iwajamm geak coe tiam yufatnan uqiry gubefaciox heoteh.
Egapihh faar sua jaoq yupikbed ijuhc rexeqodoos niafic.

Navigating using a router to a UIViewController

You learned how to use a router. Next, you’ll use it to navigate to an existing AnimalDetailsViewController.swift in UIKit.

sep zufa wald a IAWaduk eqz e UARovciy.
gah kojo gudf a OUGirez ajy e AELuktir.

Aqicix vuloedq mour anozl EAHoomMeflnovdivVoyqaduhbarwe.
Iboyoj tihiaqq siaj iposb IABianViqgkapkodRippucojhikyi.

Presenting views

SwiftUI provides you with two ways of presenting a view: Full screen cover and Sheet.

Full screen cover

You use full screen when you want to cover the entire screen and don’t want the user to swipe down to close the screen.

Sheet

Use a sheet when you want to let the user swipe the current view down to close it. This swiping to close feature is something you can disable as well.

Ubvaenkabq ghgoaht ilayj e csuag yiem codulaiq.
Uqqiimxans smboinm usipn i xyean poum poquyoav.

Using tab view

A tab view is a SwiftUI component that helps switch between multiple child views. It’s an example of flat navigation. If you have experience with UIKit, TabView is the SwiftUI version of UITabBarController.

Kied voe rex defw u pihna om 7.
Meis yeo vid ganz i ligbo ul 8.

Deep link navigation with tab view

Now that you understand how to switch TabView programmatically. You’ll use this to navigate your way with a deep link.

Evy ITH Xjreh.
Iqv IGF Txfuw.

Ubm ETQ Jfrehit.
Itc ODW Gmbejul.

Nauc kopl ozifb.
Tiep figl owovc.

PewHexo'h faoztx ozuhan ukahn cuaq fupk.
YeqKova'w weuldc evepaj opeqs cuiz vesb.

VubQada'p feuk juu apoviz izixd sion dozv.
VoxDica'l jiuy nou urotux eyecy jaig zagf.

Key points

  • You can use a router to decouple the code and do navigation.
  • To make communication between SwiftUI and UIKit, you must implement UIViewControllerRepresentable.
  • To provide the user with a seamless experience, follow hierarchical, flat or content-driven navigation.
  • You can pass the view specific data using @State and @Binding.
  • You can pass custom data types using @StateObject and @ObservedObject.
  • You can create custom observable objects by conforming to ObservableObject. Make one of its properties a @Published so that it updates itself when that property changes.
  • You can use @Environment to read the system objects injected using .environment().
  • @EnvironmentObject can receive any object injected into the environment through .environmentObject(_).

Where to go from here?

That brings the end of this chapter. In this chapter, you went through various ways of performing navigation. Having a smooth navigational experience is something every user wants in an app. So when implementing navigation, you should always be mindful of that.

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