Modern UIKit: Stack Navigation, Part 2

Episode #288 • Jul 22, 2024 • Subscriber-Only

We round out our stack navigation tools with support for an @Environment-like feature for holding onto the stack’s path, a NavigationLink-like feature for pushing features onto the stack from anywhere, and we’ll handle every corner case from deep-linking to user dismissal.

Previous episode
Modern UIKit: Stack Navigation, Part 2
Next episode
Locked

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Sign in with GitHub

Introduction

Now, it’s pretty cool we can use bindings like this, but also we probably wouldn’t want follow this pattern literally in a real world code base. We now need to pass this binding through every layer of our application so that child features can perform navigation.

When confronted with this kind of problem in SwiftUI there are two common approaches. One is to put the path binding in the environment so that every view has immediate access to it, and the other is to provide tools to the view that allow it to push values to the path without actually having access to the path.

Stephen

We are going to discuss both of these approaches, but we will start with the first one, where we put the path binding in the environment.

Of course, you are probably thinking how does one use the SwiftUI environment in UIKit?

Well, you don’t really. Instead, UIKit has its own version of the environment, but its called “traits”, and traits even bridge to SwiftUI’s environment if you want. Traits make it possible to set values inside a view or controller hierarchy, and each layer of the hierarchy will get access to the values and even be able to override the values.

Let’s take a look.

Trait system navigation


References

Downloads

Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 64 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing