1

I want to implement a feature where a view "reverts to default" if its corresponding tab item in the tab bar is tapped while the view is currently active. How can I achieve this? My TabBar is implemented like this:

VStack(spacing: 0) {
            TabView(selection: $currentTab) {
                HomeView()
                    .tag(TabBarItem.Home)
                UserAnalyticsView()
                    .tag(TabBarItem.Analytics)
                UserProfileView()
                    .tag(TabBarItem.Profile)
            }
            Divider()
            HStack(spacing: 0) {
                ForEach(TabBarItem.allCases, id: \.self) { item in
                    TabBarButton(item: item, currentTab: $currentTab)
                }
            }
            .padding(.horizontal, 15)
            .padding(.top, 10)
            .padding(.bottom)
        }

For example, if a user is inside a NavigationLink on the homepage, and they were to click the home button on the tabview, it would revert out of the NavigationLink and back to the top of the homepage

fballjeff
  • 302
  • 3
  • 9
  • Are you asking how to reset your view, or how to detect the second tap on the already selected tab? – ChrisR Feb 03 '22 at 23:17
  • @ChrisR the resetting view part, I think I can detect the second tap – fballjeff Feb 03 '22 at 23:21
  • Then you should look into `NavigationLink(isActive: )` and/or `NavigationLink(tag: selection: )`. These let you programmatically control navigation links. But it might become tricky if your nav hierarchy is deep. – ChrisR Feb 03 '22 at 23:30
  • https://stackoverflow.com/questions/57576918/swiftui-how-to-switch-to-a-new-navigation-stack-with-navigationviews or even more you case: https://stackoverflow.com/questions/60109986/pop-to-root-view-using-tab-bar-in-swiftui – ChrisR Feb 03 '22 at 23:37

0 Answers0