Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JaeHwanWO/226e021aca975631f42749e9171df31e to your computer and use it in GitHub Desktop.
Save JaeHwanWO/226e021aca975631f42749e9171df31e to your computer and use it in GitHub Desktop.
SwiftUI Tabbar and Navigation Mixed - Pop to Root doesn't work
import SwiftUI
class Views: ObservableObject {
@Published var stacked = false
}
struct ContentView: View {
@ObservedObject var views = Views()
@State var selection: Int = 0
var body: some View {
NavigationView {
TabView(selection: $selection) {
ContentView2()
}
}
.environmentObject(views)
}
}
struct ContentView2: View {
@EnvironmentObject var views: Views
var body: some View {
NavigationLink(destination: ContentView3(), isActive: $views.stacked) {
Text("Go to View 3")
}
.isDetailLink(false)
.navigationBarTitle("View 2")
}
}
struct ContentView3: View {
@EnvironmentObject var views: Views
var body: some View {
VStack {
NavigationLink(destination: ContentView4()) {
Text("Go to View 3")
}
Button("Pop to root") {
self.views.stacked = false // By setting this to false, the second view that was active is no more. Which means, the content view is being shown once again.
}
}
.navigationBarTitle("View 3")
}
}
struct ContentView4: View {
@EnvironmentObject var views: Views
var body: some View {
Button("Pop to root") {
self.views.stacked = false // By setting this to false, the second view that was active is no more. Which means, the content view is being shown once again.
}
.navigationBarTitle("View 4")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment