|
Hi there - I've spent the last few months on and off trying to figure this out...example project below. Most apps, including Apple's own, pop the user to the root view when the tab is selected (if already on a given tab, tapping it will take you to the root view). See example of what I'm trying to build here. I have not found a full implementation of this behavior in SwiftUI. It seems this feature either doesn't exist yet (are all these other apps relying on UIKit?) or I'm just struggling to find the relevant articles (help!). The example code below is an implementation that roughly works on Tab2 as it should. Tab1 at first appears to work, but the NavigationLinks break down due to the isActive binding being generic - the NavigationLink child view you get is seemingly at random. Tabs don't respond to 'onTapGesture' and this has been the closest implementation I've found. Some resources I've found that have been helpful: Paul H. has an implementation using NavigationStack and path that works great if you're using navBar buttons, but I wasn't able to make the leap to Tabs as they don't respond to .onTapGesture. Ideally a tap on a tab that you're on would clear the path and pop you back to the root: https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-programmatic-navigation-in-swiftui
|
|
While the is no way for access the button tapped on TabBar so you would have to make a custom TabBar View
Make a file called
then make a
Now in
Now when you tap on the "Home" it bring back "Page 1" Just added this for test Navigation so you can test it yourself
|
|
Nigel! This is brilliant, thanks - it works perfectly. I evolved your solution a bit to have separate 'path' @State vars to keep the different tabs from all popping back to root when one does, will post up the evolved solution when I'm done tweaking it for my needs. One thing I noticed when playing around with your solution, when it's doing the actual 'pop' back, the screen blanks out to a solid white background rather than sliding out the view itself. This isn't too noticable when using a generic white background for all views, but when you customize the background to be a solid color, the flash of white is very noticeable. Any idea what's happening there? I'm guessing this has something to do with how you pop back...the 'action()' button kicks out a Void, is that throwing an empty view that Xcode renders as blank? I wonder if this would react differently if each move through a child view appended to the $path, with action performing a .removeAll or something. Will play around with that. A few modifications to the CustomTabView(), in case they matter:
|
|
Then
only reset the "Home" Nav stack
This is proberly to do with the |
|
The 'tap twice' behavior is what I'm looking for! As for the the screen blanking out...some better google keywords led me to this stackoverflow: https://stackoverflow.com/questions/77306129/pop-view-from-navigationstack-animation-issue-in-swiftui It was a bug in iOS 17.0 that has now been fixed. Running on a real device with iOS 17.1 rather than the simulator show the views sliding in and out as expected without any flashes to white (or black if you're in dark mode). Thanks again for the clear walkthrough here, major help. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.