How to create a tab bar on iOS?

14,389

Solution 1

You can create a UITabBarController programmatically in applicationDidFinishLaunching and set it as the root view controller (or if you prefer, you can present it as a modal view). Here is the minimal code to do it:

UITabBarController *tabBarController = [[UITabBar alloc] init];

UIViewController *controller1 = [[YourViewController alloc] init];
UIViewController *controller2 = [[YourOtherViewController alloc] init];

tabBarController.viewControllers = [NSArray arrayWithObjects:
    controller1,
    controller2,
    nil];

// set as the root window
window.rootViewController = tabBarController;

If you want to customize the look of the tab bar items, do so by adding overloading (UITabBarItem *)tabBarItem in the child view controller(s):

- (UITabBarItem *)tabBarItem
{
    return [[UITabBarItem alloc] initWithTitle:@"Amazing" image:[UIImage imageNamed:@"Blah.png"] tag:0];
}

Solution 2

How to make a tab bar controller by me

  1. Drag tab bar controller into storyboard (hopefully you have one)
  2. Control-drag from tab bar controller to each view you want hooked up to it
  3. Pop bottles

enter image description here

Just so you know, this gives you the default tab bar controller behavior (so it will always be present and you can click from any page to another). If that's not what you want, then don't use a tab bar controller. To do otherwise is an abomination.

Share:
14,389
user1511244
Author by

user1511244

Updated on July 29, 2022

Comments

  • user1511244
    user1511244 almost 2 years

    I need on my main view controller to have a tab bar with tabs to navigate to all my other controllers. I just need the tab bar on this controller and when i get to another controller i just need to have a back button to go to the main controller. Now i have some questions. I created the tab bar in the main view controller and all the tabs with the text and images that i need. However i see that i can only create IBOutlet for the tab bar and not IBActions for every tab(as i thought). So i created an IBOutlet and connected it to my tab bar.

    How can i refer to every tab?

    If i can refer to every tab how is it possible to change the view controller when a tab is selected when i cant use an action about it?(I am not asking for the code to change controllers , i am asking for the place that i should put the code so that my application knows that this specific tab was pressed and has to change controller). Thank you for reading my post :D

  • user1511244
    user1511244 almost 12 years
    i dont have a storyboard and i dont want to use one. I only want the tab bar to be shown on my main view controller so that i can navigate to other controllers! On the other controllers i ll just have a back button to return to the main controller. Thank you very much for trying to help really appreciate it :) . Do you have any idea how i can do what i am describing in the simplest way? I guess there should be a function or smth that is associated with the tab bar i created so that when u hit a tab the code runs there and depending on the tab u hit u do the corresponding action right?
  • Dustin
    Dustin almost 12 years
    Storyboards are the best, you should use them. If you want to do everything programmatically, then read stackoverflow.com/q/3923465/1487063. What you want the tab bar to do is kind of frowned upon by Apple developer.apple.com/library/ios/#documentation/userexperienc‌​e/…. To get what you want in a way that won't get rejected, just add some buttons and use presentModalViewController. For a back button, use dismissModalViewController
  • user1511244
    user1511244 almost 12 years
    pff i cant believe its so hard to do this. the stackoverflow link doesnt exactly show what i am asking :\
  • Dustin
    Dustin almost 12 years
    Everything is hard without storyboards. Everything but curling up into a ball and crying softly to yourself until they come to take you away. You should use them.
  • user1511244
    user1511244 almost 12 years
    I need to support iOS versions that dont use them.. ahhaha great comment by the way!
  • Dustin
    Dustin almost 12 years
    Well look at this code sites.google.com/site/iphonesdktutorials/sourcecode/… Just so you know, iOS5 has an 85% adoption rate and if you're new to coding, just don't support anything older. We're only a few months away from iOS6, so there's a very, very small chance supporting older iOS systems is significant. My company dropped iOS < 5 support several months ago.
  • user1511244
    user1511244 almost 12 years
    yes and i completely agree with u but my boss has a different point of view :D
  • Dustin
    Dustin almost 12 years
    Well look at that source code, show your boss some numbers, and good luck.
  • user1511244
    user1511244 almost 12 years
    Thanks a lot Dustin for all your help :)
  • user1511244
    user1511244 almost 12 years
    Dustin a last question so that i dont search for nothing. With a tab view controller i can switch between controllers right? Not just between nib files that are associated with the same controller?
  • Dustin
    Dustin almost 12 years
    You can. Read the apple doc on the subject developer.apple.com/library/ios/#documentation/WindowsViews/‌​… Switching between different view controllers is the ONLY thing it can do.