Tab bar controller inside a navigation controller, or sharing a navigation root view

75,220

Solution 1

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.

Solution 2

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!

Solution 3

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.

Solution 4

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.

Solution 5

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];
Share:
75,220

Related videos on Youtube

Daniel Dickison
Author by

Daniel Dickison

I love Lisp, finding Swift pretty great, and I'd probably get hooked on Rust if I found some time to get to know it. Also: I don't hate JavaScript.

Updated on July 05, 2022

Comments

  • Daniel Dickison
    Daniel Dickison almost 2 years

    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.

  • Anh
    Anh over 13 years
    For those wondering, the link to the mentioned blog post is wiredbob.com/blog/2009/4/20/…
  • A Salcedo
    A Salcedo about 13 years
    @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?
  • Izac
    Izac about 13 years
    @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).
  • bvanderveen
    bvanderveen over 12 years
    How does your custom tab bar controller class handle plumbing for the navigationItem and navigationController properties of its child controllers?
  • sparkFinder
    sparkFinder about 12 years
    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.
  • Gargo
    Gargo over 11 years
    you fogot to release your UIViewControllers
  • darksider
    darksider over 11 years
    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
  • Vijay Katam
    Vijay Katam over 11 years
    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.
  • Tiago Veloso
    Tiago Veloso about 9 years
    The blog post is dead for me, any chance some one has that lying around?
  • oluckyman
    oluckyman almost 6 years
  • Koen.
    Koen. over 3 years
    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.