DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • 4 Email Validation and Verification Techniques for App Developers
  • A Developer’s Guide to Multithreading and Swift Concurrency
  • Unlock AI Power: Generate JSON With GPT-4 and Node.js for Ultimate App Integration
  • How to Make a Picture-in-Picture Feature in iOS App Using AVFoundation

Trending

  • Is the Model Context Protocol a Replacement for HTTP?
  • How to Format Articles for DZone
  • Beyond Microservices: The Emerging Post-Monolith Architecture for 2025
  • How Clojure Shapes Teams and Products
  1. DZone
  2. Data Engineering
  3. Data
  4. Complex Navigation in SwiftUI

Complex Navigation in SwiftUI

Explaining the problems experienced when creating complex Navigation in SwiftUI.

By 
Bruno Basas user avatar
Bruno Basas
·
Dec. 16, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
4.5K Views

Join the DZone community and get the full member experience.

Join For Free

Navigation in SwiftUI has been a major focus of the framework from day one; however, when we tried to create an app that had a bit more navigation and view complexity, we ran into the first problems.

Given the importance of navigation within an app, it has been revised and improved. In this WWDC 22′, they have published a new API to build complex navigation flows, making development easier.

To see its use, we are going to build a small shopping list app and apply the new navigation changes to it.

Shopping List

The example application is simply a shopping list where we can see different products, and when we click on it, we will navigate to their details.

Shopping List


Prior iOS 16

Example of how the navigation settings of our app would look like before receiving iOS16 enhancements.

Navigation Settings


We all recognize the NavigationView object that has brought us so many headaches. The first thing we are going to do is to change it since it has been deprecated for an improved version of this one.

NavigationStack (NEW)

A view that displays a root view and enables you to present additional views over the root view.

Root View


As we have commented before, the navigation of our example app is a master-detail navigation, and as you can see, it still uses NavigationLink for each product in the list. It fulfills its purpose and does not mean that it is badly used; we will simply see that we will be able to improve it.

With iOS16, we are introduced to a new way of how approaching navigation, the new navigationDestination view modifier.

.navigationDestination(for:destination:) (NEW)

Associates a destination view with a presented data type for use within a navigation stack.

This new modifier allows us to indicate the value we want to display along with the view we want to display for that particular value. We place it as we would any view modifier, specify the type, and within the closure, we will specify the view to which we want to navigate.

Destination View


Finally, we must modify the init of the NavigationLink to be able to pass the value to it.

The best of all is that the NavigationStack works as a stack of views, an array in which the types (and, in turn, the views to which it is linked) are added in order, and this array is exposed to us so that we can play with it. Apart from the array of products to popularize the view, we are now going to declare an array as @State; we will call it path.

Path


When we initialize the NavigationStack we can now pass it to the array as publisher.

Pass it on


This change allows us to modify the navigation stack as we wish. For example, we can create a function to navigate directly to the offer of the day or the classic method to return to the beginning of the navigation stack. As easy as editing an array.

Creating a Function


The Three Rules of NavigationDestination

When using the .navigationDestination modifier, we have to take into account these three rules.

  1. The navigationDestination modifier must always be inside a NavigationStack.
  2. We should not put the navigationDestination modifier as a child of any element considered as lazy container, such as List, LazyVStack, or LazyVGrid, because these elements do not load the views immediately.
  3. The navigationDestination declared above will always overwrite those below it that conform to the same type.
API Data structure app Apple iOS Swift (programming language)

Published at DZone with permission of Bruno Basas. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • 4 Email Validation and Verification Techniques for App Developers
  • A Developer’s Guide to Multithreading and Swift Concurrency
  • Unlock AI Power: Generate JSON With GPT-4 and Node.js for Ultimate App Integration
  • How to Make a Picture-in-Picture Feature in iOS App Using AVFoundation

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!