Previewing a view in a NavigationStack
Some views are designed to be embedded in a NavigationStack
; however, the NavigationStack
is usually declared in the parent view and not our view. Following best coding practices, our view is a separate struct usually declared in a separate file. When we preview our view, we would like to see how it would look embedded in a NavigationStack instead of as an isolated view. One solution to this problem would be to run the application and navigate to the view in question. However, previews provide a time-saving way to view UI changes live without rebuilding the app.In this recipe, we will create an app with a view that is part of the navigation stack, previewing it in a NavigationStack
instead of as an isolated view.
Getting ready
Let's create a SwiftUI app called PreviewingInNavigationStack
.
How to do it…
We will add a NavigationStack
and NavigationLink
component to the ContentView
struct. The NavigationLink
opens a second view that...