1

I have a TabView with four tabs they look like this:

enter image description here

For example on Search tab user can search products (presented in a List) and then click on product (simple NavigationLink) to open up more informations (ProductInfoView) about that product (note Search tab will remain selected).

So my question is:

if user clicks again on Search tab, which is still selected, how can I pop up or dismiss ProductInfoView and return to Search tab?

Facebook has same functionality. Note also that: click on unselected tab will dismiss ProductInfoView and show tab I just selected but clicking again on already selected tab in this case Search while ProductInfoView is open will not do anything.

I have found similar answer here: SwiftUI: How to pop to Root view

Probably I could implement that solution but I didn't find a way how to detect user click on already selected tabItem. I know that UITabBarController has function which probably can do that:

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {    
}

Maybe answer is simple but I'm still new in this field and struggling to do this. Thanks in advance.

Yupi
  • 4,402
  • 3
  • 18
  • 37

1 Answers1

2

The solution is actually simple, just keep a track of previously selected tab and when you get the current tab in the following method

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

}

check if currentTab == previouslySelectedTab, if yes just pop the navigation controller (currentTab.selectedViewController) to root. It should work seamlessly.

abhinavroy23
  • 3,630
  • 1
  • 14
  • 19