80

I'm trying to implement a UI structured like in the Tweetie app, which behaves as so: the top-level view controller seems to be a navigation controller, whose root view is an "Accounts" table view. If you click on any account, it goes to the second level, which has a tab bar across the bottom. Each tab item shows a different list and lets you drill down further (the subsequent levels don't show the tab bar).

So, this seems like the implementation hierarchy is:

  • UINavigationController
    1. Accounts: UITableViewController
    2. UITabBarController
      1. Tweets: UITableViewController
        • Detail view of a tweet/user/etc
      2. Replies: UITableViewController
      3. ...

This seems to work[^1], but appears to be unsupported according to the SDK documentation for -pushViewController:animated: (emphasis added):

viewController: The view controller that is pushed onto the stack. It cannot be an instance of tab bar controller.

I would like to avoid private APIs and the like, but I'm not sure why this usage is explicitly prohibited even when it seems to work fine. Anyone know the reason?

I've thought about putting the tab bar controller as the main controller, with each of the tabs containing separate navigation controllers. The problem with this is that each nav controller needs to share a single root view controller (namely the "Accounts" table in Tweetie) -- this doesn't seem to work: pushing the table controller to a second nav controller seems to remove it from the first. Not to mention all the book-keeping when selecting a different account would probably be a pain.

How should I implement this the Right Way?

[^1]: The tab bar controller needs to be subclassed so that the tab bar controller's navigation item at that level stays in sync with the selected tab's navigation item, and the individual tab's table controller's need to push their respective detail views to self.tabBarController.navigationController instead of self.navigationController.

Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89

9 Answers9

63

The two previous answers got it right - I don't use UITabBarController in Tweetie. It's pretty easy to write a custom XXTabBarController (plain subclass of UIViewController) that is happy to get pushed onto a nav controller stack, but still lives by the "view controller" philosophy. Each "tab" on the account-specific view (Tweets/Replies/Messages) is its own view controller, and as far as they are concerned they're getting swapped around on screen by a plain-ol UITabBarController.

Mutawe
  • 6,464
  • 3
  • 47
  • 90
Loren
  • 973
  • 8
  • 10
  • 2
    How does your custom tab bar controller class handle plumbing for the navigationItem and navigationController properties of its child controllers? – bvanderveen Sep 23 '11 at 21:59
  • Has this changed in iOS 5? I seem to be able to add a UITabBarController into a UINavigationController without too much of a problem, even as a the root controller. – sparkFinder May 08 '12 at 20:54
33

I'm building an app that uses a similar navigation framework to Tweetie. I've written a post about how to do this on my blog www.wiredbob.com which also links to the source code. It's a full template you could take and use as a basis for another project. Good luck!

Robert Conn
  • 877
  • 8
  • 17
  • 10
    For those wondering, the link to the mentioned blog post is http://www.wiredbob.com/blog/2009/4/20/iphone-tweetie-style-navigation-framework.html – Anh Aug 26 '10 at 12:10
  • @Robert Conn, really nice tutorial. I am using your concept, but for some reason my tabBar is not active. Did this happened to you when you were implementing your app? – A Salcedo Feb 15 '11 at 23:36
  • @ASalcedo: I too had this problem but found that it was because I needed to reduce the size of the views associated with the tab bar items, the default height size of 460 was to large, in the source code at www.wiredbob.com you will see that the height set for the tab items is 369 to allow for the tab bar. In order to change the height in interface builder you also have to ensure that status, top and bottom bar are all set to none in the attributes inspector of the view(not immediately obvious). – Izac Apr 20 '11 at 12:33
  • 2
    The blog post is dead for me, any chance some one has that lying around? – Tiago Veloso May 05 '15 at 10:20
  • working blog post link: http://www.wiredbob.com/2009/04/iphone-tweetie-style-navigation.html – oluckyman May 28 '18 at 10:44
  • You shouldn't link to an external resource for the answer. Instead you should provide the answer here, and refer to an external resource for more in-depth information for example. – Koen. Oct 27 '20 at 15:29
10

It's possible to add a UITabBar to any UIViewController. That way you don't actually have to push a UITabBarController and therefore stay within the guidelines of the Apple API.

In interface builder UITabBar is under "Windows, Views & Bars" in the Cocoa Touch Library.

Himadri Choudhury
  • 10,217
  • 6
  • 39
  • 47
  • 1
    Hi! @Himadri Choudhury can you explain "and therefore stay within the guidelines of the Apple API" - or give the reference ? I can't find where this guideline is told (I don't doubt it exists, but I can't find the URL). Thank you very much – darksider Sep 28 '12 at 14:02
10

I do this in a couple of my apps. The trick to adding a tab bar to a navigationController based app is to NOT use a TabBarController. Add a Tab Bar to the view, make the view controller for that view a TabBarDelegate, and respond to user selections on the tab bar in the code of the view controller.

I use Tab Bars to add additional views to the Tab Bar's view as sub-views, to reload a table view with different datasets, to reload a UIPickerView, etc.

Alpinista
  • 3,751
  • 5
  • 35
  • 46
6

I was struggling for the past hour to implement a UITabBar because it would get hidden when I tried to display my view; then I found this post:

Basically, make sure you insert your new view below the tabbar, per this line of code:

[self.view insertSubview:tab2ViewController.view belowSubview:myTabBar];
Mutawe
  • 6,464
  • 3
  • 47
  • 90
esilver
  • 27,713
  • 23
  • 122
  • 168
  • Thanks this helped fix my disappearing tab bar. I used the same code that Robert Conn proposed but somehow the ui tab bar disappears when I switch to the second view. – Vijay Katam Oct 22 '12 at 04:28
3

This could be achieved by simply embedding the TabBarController in the Navigation Controller. In the storyboard:

  1. Drag a ViewController
  2. Click on the ViewController's Scene
  3. Click on editor >> Embed in >> Navigation Controller.
  4. Drag a button on the same ViewController.
  5. Drag a TabBarController
  6. Connect the button on the ViewController to the TabBarController via push Segue Action.

In this case only the TabBarController's RootViewController would be in the Navigation Controller's stack. All The TabBarItems would have the Navigation Bar at the top and user can go to Home Screen at any time, irrespective of the selected TabBarItem

This could be done at any ViewController in the Navigation Controller's stack.

If it works, please suggest me how to increase the reputation so that I can post the images and the code in the next answer. :)

caffieneToCode
  • 602
  • 5
  • 15
3

In my app, the root view controller is a UINavigation controller. At a certain point in the app, I need to display a UITabBar. I tried implementing a UITabBar on a UIView within the navigation hierarchy, as some of the previous posts suggested, and this does work. But I found that I wanted more of the default behavior that the tab controller provides and I found a way to use the UITabBarController with the UINavigation controller:

1) When I want to display the UITabBarController's view, I do this:

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.window.rootViewController = myUiTabBarControllerInstance;

2) When I want to return to where I was in the navigation hierarchy, I do this:

appDelegate.window.rootViewController = myNavControllerInstance;
richardsun
  • 3,245
  • 1
  • 18
  • 22
3

This is how i did it. This is actually pushing a tabbarcontroller onto navigation controller. It works fine. I didn't find anywhere in the documentation that apple doesn't support this way. Can someone give me link to this warning?

If this is truth, is it possible that apple refuses to publish my app to appstore?

-(void)setArrayAndPushNextController
{
    MyFirstViewController *myFirstViewController = [[MyFirstViewController alloc] init];
    MySecondViewController *mySecondViewController = [[MySecondViewController alloc] init];

    myFirstViewController.array = self.array;


    NSArray *array = [[NSArray alloc] initWithObjects:myFirstViewController, mySecondViewController, nil];
    UITabBarController *tab = [[UITabBarController alloc] init];
    tab.viewControllers = array;

    [array release];

    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"first title" image:nil tag:1];
    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"second title" image:nil tag:2];

    myFirstViewController.tabBarItem = item1;
    mySecondViewController.tabBarItem = item2;

    [self stopAnimatingSpinner];

    [self.navigationController pushViewController:tab animated:YES];

    [tab release];
    [item1 release];
    [item2 release];
}
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
savudin
  • 45
  • 1
  • 7
1

I wrote a blog post on how I approached this problem. For me, using a modal view was a simpler solution than writing a custom tab-bar implementation.

http://www.alexmedearis.com/uitabbarcontroller-inside-a-uinavigationcontroller/

dst
  • 1,770
  • 13
  • 26
Alex Medearis
  • 176
  • 1
  • 7