fraggjkee
fraggjkee

Reputation: 3614

Init UINavigationController with UITabBarController as root

I have a root UINavigationController and want to init it with an instance of UITabBarController, something like this:

TabBarController * viewController = [[TabBarController alloc] init];
UINavigationController navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];

As per the documentation for initWithRootViewController: method, this is a bad idea:

The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class.

So I wonder:

  1. Why do we have such a restriction?
  2. ...why does it work and doesn't throw any exceptions? Are there any side effects of such approach? I'm trying to reuse the single navigation controller across all tabs of my tab bar controller and so far the code from above works quite well.

What I need is to 1) have a consistent NavBar in all of my tabs (but with different titles and left/right icons 2) Some tabs must support drill-down navigation 3) I don't need the tab bar panel when switching to deeper elements of the screens hierarchy.

EDIT

I've just realized that Skype for iOS is a good example of what I am trying to achieve: it works exactly like my app in terms of tabs and navigation.

Upvotes: 1

Views: 741

Answers (1)

Mundi
Mundi

Reputation: 80265

I suppose the reason is that if you push another view controller onto the navigation stack, it would push the tab bar controller to the side in a standard animation, resulting in a potentially confusing user experience.

As you switch to different controller with the tab bar, you would be pushing the next view controller on top of different controllers, at least from the perspective of the user - this can be quite perplexing.

The tab bar is really meant to be always there, so it does not make much sense to embed it into another controller of controllers. You can hide the bottom bar for specific screens if necessary.

The fact that ultimately it is just a controller of controllers is also the reason why it works. The controller of controllers is oblivious about what kind of controllers it controls.

If you describe your exact use case, you be able to get more specific advice if this is a good idea in your case or not. Perhaps you might want to consider using a UIToolbar instead.

Upvotes: 3

Related Questions