Skip to content

Instantly share code, notes, and snippets.

@fraune
Last active October 26, 2023 03:19
Show Gist options
  • Save fraune/114e7321b6b60ee85e5d455738f18be1 to your computer and use it in GitHub Desktop.
Save fraune/114e7321b6b60ee85e5d455738f18be1 to your computer and use it in GitHub Desktop.
SwiftUI NavigationStack nested within TabView
//
// ContentView.swift
// MixedViewsTest
//
// Created by Brandon Fraune on 8/11/23.
//
import SwiftUI
/// https://stackoverflow.com/a/76887384/13944515
struct ContentView: View {
var body: some View {
TabView {
AirplaneView()
.tabItem {
Label("Airplane Tab", systemImage: "airplane.circle")
}
PlanetView()
.tabItem {
Label("Planet Tab", systemImage: "globe.americas.fill")
}
}
}
}
struct AirplaneView: View {
var body: some View {
NavigationStack {
VStack {
Text("Airplane tab view")
Divider()
NavigationLink(destination: NestedItemA()) {
Text("Go to nested airplane view")
}
.navigationTitle("Airplane")
}
}
}
}
struct PlanetView: View {
var body: some View {
NavigationStack {
VStack {
Text("Planet tab view")
Divider()
NavigationLink(destination: NestedItemB()) {
Text("Go to nested planet view")
}
.navigationTitle("Planet")
}
}
}
}
struct NestedItemA: View {
var body: some View {
NavigationStack {
Text("Airplane goes vroom")
.navigationTitle("Nested airplane")
}
}
}
struct NestedItemB: View {
var body: some View {
NavigationStack {
Text("Planet spin good")
.navigationTitle("Nested planet")
}
}
}
#Preview {
ContentView()
}
@fraune
Copy link
Author

fraune commented Oct 25, 2023

Demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment