UITabBarController repeatedly switches to first tab

This seems to be similar or related to the issue mentioned here.

When creating a UITabBarController that is not the initial view controller, eventually and permanently, the UITabBarController starts to switch to the first tab when tapping anywhere on the screen. However, the tap event is clearly recognized by the initial tab when I switch back to it from the first (a view indicates it was tapped).

There are no tapGestureRecognizers anywhere in my code except for that found in a well-known third party library that does not have any known bugs relating to this issue. The issue also applies in the third tab that has no tapGestureRecognizers at all (the first which is defaulted to doesn't either).

I also get the same sort of behavior as in the above question when repeatedly pressing tabs - switches back and forth between the tabs in a somewhat unpredictable fashion when tapping them directly.

The TabBarController's initialIndex is clearly set in the viewDidLoad for that controller.

When the issue emerges, tapping the screen anywhere tabBarController(_:shouldSelect:) and tabBarController(_:didSelect:) are called, but I haven't been able to figure out the call stack apart tracing it to some assembly in a dylib or indeed why.

I'd really appreciate someone else's advice on this issue - I don't know if manually setting the tabs on initialisation is a solution, or if this suggests a memory leak or horrible memory issue. I don't think it's a threading issue because everything is currently done on the main thread.

The issue is very hard to reproduce as the amount of time it takes is unpredictable, but has been observed on both a real iPhone 7 and on the simulator (macOS 15.6) for an iPhone 12 and iPhone SE 2nd gen (all on iOS 14.1).

There are no tapGestureRecognizers anywhere in my code except for that found in a well-known third party library that does not have any known bugs relating to this issue.

How can you be so sure ?
If you want to force the selected tab, look here
https://stackoverflow.com/questions/25325923/programmatically-switching-between-tabs-within-swift
and here
https://stackoverflow.com/questions/52178000/tapping-on-a-tabbar-item-should-always-open-the-first-view-controller
for some hints.

How can you be so sure ?

I ran grep -R "ecognizer" MyProject and there were zero results. Conversely when running the same command on my CocoaPods directory I got multiple hits. That grep command would also include anything in .storyboard files, so I am pretty confident in this.

If you're asking how I'm sure about the third party library not being the issue, then I'm less sure about this. I haven't found any open bugs with similar issues there though, and it's been around for 6 years now.


Have you a mean to extract a simplified project without the library ?

Another idea (low probability):
  • does it happen wherever you tap on screen, or only on certain parts ?

  • check all the parameters of the tabBar (in IB). May be some size is too large and cover other parts of the screen ?

  • did you debug the view hierarchy when running in simulator ?

Have you a mean to extract a simplified project without the library ?

I am trying to do so and will update when I do. It’s very hard to know how long I should give it before guessing if the bug isn’t there.

To the other points:

does it happen wherever you tap on screen, or only on certain parts ?

Anywhere.

check all the parameters of the tabBar (in IB). May be some size is too large and cover other parts of the screen ?

They don’t, but that would only make sense if the issue started appearing immediately, which it doesn’t. It takes some time to emerge.

did you debug the view hierarchy when running in simulator ?

I did, and the view hierarchy does seem to suggest the first tab doesn’t contain its image/label inside the content view, but on the same level of nesting. I don’t know what the ‘correct’ hierarchy looks like. Nothing else stands out.


Dansoutar. Having the exact same problem you describe - did you ever find a solution? Anything to share...thanks in advance! Regards, Jules.

UITabBarController repeatedly switches to first tab
 
 
Q