0

I am trying to use UITabBarController in swift 4.2: https://developer.apple.com/documentation/uikit/uitabbarcontroller to navigate between 2 views, lets say ViewController1 and ViewController2 , each of these controllers being embedded in his own navigation controller. I am using swift 4.2 and deployment target is IOS 12.1. (Image with storyboard and controllers)

But it seems the default behavior is that to go to another tab you need 2 clicks(taps) as described also in Why i need to tap tabBarViewController item Twice to navigate to different View Controller - iOS

At first I thought it is something that I am doing wrong, but also trying the template provided by the IOS team for the tab bar it seems that the workflow is the same.

I created the UITabBarController from the storyboard. I also tried creating a custom tab bar class but did not manage to make it work. If I click once on ViewController2's tab, the viewDidLoad() and viewDidAppear() for ViewController2 are being called but the UI is not refreshed. After clicking anywhere in View or try to scroll the View is refreshed with the correct ViewController2.

Can you help me with this issue please? How can I make UITabBarController respond to a single click for a tab?

  • Your question isn't clear. You say you have *"ViewController1 and ViewController2 that are embedded in a navigation controller"* and you want the tabBar buttons to navigate between them? Tabs should be used to switch to different view controllers, ***NOT*** to try to push/pop controllers in a navigation controller. – DonMag Mar 08 '20 at 13:31
  • Sorry I did not explain better, I meant each of the view controllers is embedded in his own navigation controller. Let me know please if now it makes sense. I edited the question also – Victor Mitris Mar 08 '20 at 13:59
  • I also added an image with the desired storyboard and controllers to make it more clear – Victor Mitris Mar 08 '20 at 14:27
  • OK - you have 2 tabs, each with a Navigation Controller (NavVC), and each NavVC has ***ONE*** view controller. Tapping the first tab should show `FirstView` embedded in a NavVC and tapping the second should show `SecondView` embedded in a NavVC. What else would you expect to happen? – DonMag Mar 08 '20 at 14:38
  • Yes, but to show FirstView it needs 2 taps as well as to show SecondView. I want to show them by only tapping only once each tab bar item. From https://stackoverflow.com/questions/47173892/why-i-need-to-tap-tabbarviewcontroller-item-twice-to-navigate-to-different-view I understand that this is the default behavior but how can I customize it to use only one tap/click? – Victor Mitris Mar 08 '20 at 14:53
  • First, you are misunderstanding the post you linked. You do ***not*** have to tap twice on a tab to switch to that tab. I just did this: New Storyboard... add TabBarController (it automatically adds VCs for two tabs)... embed each VC in Nav Controller... put a label on each VC... run the app (***no code at all***). Single tap on each tab switches view controllers. If that doesn't work for you, then you are not telling us what else you are doing. Here is a simple example: https://github.com/DonMag/BasicTabs – DonMag Mar 08 '20 at 15:11
  • Thank you for taking time and provide this example. Before trying your example I needed some changes to it to make it compile because I have swift 4.2 and IOS 12.1. For this I commented the whole SceneDelegate.swift as it complained about use of undeclared 'UIScene' that I believe does not exist in IOS 12.1. Doing so the behavior is exactly as described, needing 2 taps/clicks to move between tabs. Do you think it is because of the older version of swift/IOS target? – Victor Mitris Mar 08 '20 at 15:27
  • Are you saying that with my example, you run the app and see the Yellow view... then you tap the second tab and you ***DO NOT*** see the Green view? – DonMag Mar 08 '20 at 15:33
  • I update that GitHub repo with the project changes for Swift 4.2 and iOS 12.1 target, if you want to refresh it. – DonMag Mar 08 '20 at 15:43
  • **Exactly**, I tap on the second bar and I do not see the Green View. If I tap again the second tab bar (or actually anywhere on the tabBar) then it show the Green View. Again, for your example I changed it to match swift 4.2 and IOS target 12.1 as this is my supported environment so I commented all SceneDelegate.swift file. Also, another mention, I am using simulator, not actual device. Is there a possibility that the simulator has a problem or on these swift/IOS versions it had a different behavior? – Victor Mitris Mar 08 '20 at 15:44
  • I tried the example with Swift 4.2 and IOS 12.1 target also. Same thing..I really do not understand what is different on my setup if it works on your for these versions also – Victor Mitris Mar 08 '20 at 15:51
  • Well, that is definitely not ***normal*** behavior. IF you have a navigation controller in a tab, and you have pushed a view (or several), then clicking again on the tab *while it is alread the selected tab* will reload that NavVC and go back to the root VC for that nav controller. But it is absolutely not the case that you have to tap a tab twice to switch to that tab. TabBarController has been around for a long, long time, and that has never been the case. If you open the default Photos app, do you have to tap twice to switch tabs? – DonMag Mar 08 '20 at 15:57

0 Answers0