How it works
StackNavigationViewStyle is a NavigationViewStyle that forces a NavigationView to present its content as a single stack of views, pushing and popping one screen at a time. By default, a NavigationView adapts its presentation to the platform and size class — on wide layouts such as iPad in landscape it may show a two-column split view. Reach for StackNavigationViewStyle when you want that consistent, drill-down stack behavior everywhere, regardless of device or orientation. You apply it through the navigationViewStyle(_:) modifier rather than instantiating it directly into a view hierarchy.
Conform to NavigationViewStyle
StackNavigationViewStyle is a concrete type conforming to the NavigationViewStyle protocol, which describes how a NavigationView lays out and presents its columns. It carries no configuration of its own — its entire behavior is to choose the stack-based, push-and-pop presentation over an adaptive or split-column one.
Construct it with the default initializer
Create the style by calling its parameterless initializer,
StackNavigationViewStyle(). There are no options to set; the instance is simply a marker that selects the stack presentation when handed to the style modifier.Apply it with navigationViewStyle(_:)
The style takes effect when you pass it to the
.navigationViewStyle(StackNavigationViewStyle())modifier on aNavigationView. The modifier hangs off the NavigationView itself — in the example it sits on the view returning fromNavigationView { ... }— so the chosen style governs how that container resolves its presentation.Drive the stack with NavigationLink
Once the stack style is in force, each
NavigationLinkpushes its destination onto the single navigation stack. In the example, the"Inbox"and"Sent"links inside theListeach push aTextdestination, and the back affordance pops them off — the classic single-column drill-down that this style guarantees.
.navigationViewStyle(StackNavigationViewStyle()) line and run on an iPad in landscape: the same NavigationView now resolves to a two-column split layout, revealing exactly what the stack style was overriding.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct StackNavigationViewStyleDemo: View {
var body: some View {
NavigationView {
List {
NavigationLink("Inbox", destination: Text("Inbox"))
NavigationLink("Sent", destination: Text("Sent"))
}
.navigationTitle("Mail")
}
.navigationViewStyle(StackNavigationViewStyle())
.padding()
}
}