UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Pop to root view using Tab Bar in SwiftUI?

Forums > SwiftUI

Hi all, I have the same question that was asked here, with no answers: https://stackoverflow.com/questions/60109986/pop-to-root-view-using-tab-bar-in-swiftui

Is there any way to pop to root view by tapping the Tab Bar?

Here's an example of the expected behavior: https://i.stack.imgur.com/RBEvL.gif

Thank you for any suggestions!

3      

Did you ever find a solution to this issue?

2      

Can't remember where I found this solution, but it does work. On the View that creates the TabView:

TabView(selection: $context.selectedTab) {
  ...generate your views normally.
  }.onReceive(context.$selectedTab) { selection in
            context.navToHome.toggle()
  }

Where context is some environmentObject that you're passing to the affected views, and navToHome is a bool.

In your child views that you want to pop back to the root from:

@EnvironmentObject var context: YourContext
@Environment(\.presentationMode) private var presentationMode

var body: some View {
  VStack {
    Text("View here")
  }.onChange(of: context.navToHome) { _ in
            presentationMode.wrappedValue.dismiss()
   }
}

You can also add other logic to the onChange that changes the behavior slightly. In my app, it only pops back to the root if it's tapped a second time, which allows viewing other tabs and retaining your state in one tab when you go back to it. Just a heads up, accessing @Environment(.presentationMode) is currently causing a bug where it causes the view to initialize multiple times, requiring you to do any initialization in other parts of code that only fire once, like onAppear. Hopefully this helps!

3      

RMcGhee, thank you for responding. I implemented your solution to the best of my ability - my code doesn't show any compiler errors but I get a fatal build error.

Error:

Fatal error: No ObservableObject of type PopToRoot found. A View.environmentObject(_:) for PopToRoot may be missing as an ancestor of this view.

I tried resolving this by implementing Paul's solution from this link and tacked this modifier onto my views: .environmentObject(popToRoot). The same fatal error still appears.

This is my environmentObject that's being passed to my views: @EnvironmentObject var popToRoot: PopToRoot Here is the class where PopToRoot is defined:

import SwiftUI
class PopToRoot: ObservableObject {
    @Published var navToHome = false
    @Published var selectedTab = ""
}

In your comment you said "Where context is some environmentObject that you're passing to the affected views, and navToHome is a bool." Can you give some more details about the environmentObject that you're passing to your views?

2      

Hi @davidzimmerjr,

You are on the right path, I just tried @RMcGhee solution and it works great.

I think you are getting a No ObservableObject of type PopToRoot found because you are not initializing your @EnvironmentObject var popToRoot before using it in your ContentView.

Either instantiate it in your App and pass it down to ContentView with .environmentObject or instantiate it with an @StateObject directly in your ContentView:

@StateObject var popToRoot = PopToRoot()

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.