Better Programming

Advice for programmers.

Follow publication

Member-only story

Modularised Navigation in SwiftUI — An Enum-based Approach

Marcel Kulina
Better Programming
Published in
5 min readJul 27, 2023

--

Source

In iOS 16, SwiftUI finally allows us to properly encapsulate navigation without being forced to pass a ton of bindings or using NavigationLink directly in UI.

One thing that always introduces additional challenges, though, is modularisation.

In a best-case scenario, every module functions independently and has zero dependencies on other modules (in the same layer).

While it is possible to achieve that, there is always the need to navigate from one module to the other.

One approach that elegantly solves this issue is the use of enums for internal and external navigation.

Concept

The App (or parts of it) are bundled within a single router. The entry point (App/Root View/TabViews) creates and holds the NavigationPathis and passes it to a NavigationStack . Since NavigationPathis a reference type, we can then create our main router and pass to it that very same path. Now all the main router needs are some operations for pushing and popping views.

As for the modules, each module contains a custom router that handles the…

--

--

Write a response