Router Pattern for SwiftUI Navigation: Sheets and Full Screen Covers
- Eric Palma
- Dec 6, 2023
- 4 min read
Updated: Mar 29
In our previous post, we explored the downsides of coupling navigation logic with views in SwiftUI and introduced the Router pattern as a solution. Now it’s time to dive in deeper and see how we can extend our Router pattern implementation so that it can present screens using a sheet, or full screen cover, rather than only relying on push navigation.
Updating our RouterView to present Sheets and Full Screen Covers
Previously, for simplicity we only concentrated on the Router's management of a NavigationStack for push navigation. As a result our RouterView only included APIs for navigation stacks.
Now, since we are expanding our Router to handle diverse navigation methods like sheets and full screen covers, we need to add the corresponding navigation APIs to our RouterView. Below is our updated code:
We have added the .sheet modifier to support presenting views in a sheet. It contains a binding to a new property in our Router called presentingSheet. We will learn more about this property in the following section.
We have added the .fullScreenCover modifier to support presenting views using a full screen cover. It too has a binding to a new property called presentingFullScreenCover. More on this property in the following section.
Just like that, with these minor additions in place, our RouterView can now support two other types of navigation in addition to a NavigationStack. We are halfway to our goal of supporting sheets and full screen covers. Now we just need to update our Router object.
Updating our Router to support Sheet and Full Screen Cover presentation
The majority of the updates we need in order to support the additional types of navigation are located in our Router object. It needs the following additions:
A new Enum representing the navigation types it supports.
Observable properties used to trigger each navigation type.
Methods to expose the new functionality to our views.
We define a simple Enum called NavigationType to represent the different navigation types supported by our Router.
Join my newsletter for exclusive iOS insights, tips, and 25% off any subscription plan!
Want to read more?
Subscribe to curiousalgorithm.com to keep reading this exclusive post.