6

I've been looking at programatically adding a tab bar to my view controller because having a scroll view I can't place it on without it being in the middle of my view. I'm abit confused about how to add it. Does it need to be initiated in my ViewDidLoad method or my AppDelegate? If I have:

UITabBarController  *tabBar = [[UITabBarController alloc] init];
[self.view addSubview:tabBar.view];
[tabBar release]; 

How can I allocate it to the bottom of my scrollview?

Thanks!

Now in my appDelegate class :

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

   self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    ViewController* vc = [[ViewController alloc] init];


    NSArray* controllers = [NSArray arrayWithObjects:vc, tabBarController, nil];
    tabBarController.viewControllers = controllers;

    [_window addSubview:tabBarController.view];

   self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
   return YES;

}

It's crashing and not sure if it's a release I'm missing.

Edit: For anyone else wondering this in Appdelegate.m:

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4];
Andrea F
  • 733
  • 2
  • 9
  • 16
  • `NSArray* controllers = [NSArray arrayWithObjects:vc, tabBarController, nil];` Why are you adding `tabBarController` ? – Bhavin Jun 11 '13 at 09:23
  • Yes I added the Edit in my question to show what worked for me – Andrea F Jun 27 '13 at 21:52
  • There was one more thing and it was : You added `tabBarController` in your array of `viewControllers` and that was wrong. That's what I have added in my Answer. – Bhavin Jun 28 '13 at 06:25

3 Answers3

8

Take a look at this Documentation given by Apple: ViewControllers.

Sample Code :

- (void)applicationDidFinishLaunching:(UIApplication *)application {
   UITabBarController *tabBarController = [[UITabBarController alloc] init];

   FirstViewController* vc1 = [[FirstViewController alloc] init];
   SecondViewController* vc2 = [[SecondViewController alloc] init];

   NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
   tabBarController.viewControllers = controllers;

   // Add the tab bar controller's current view as a subview of the window
   [window addSubview:tabBarController.view];
}
Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • just one tiny little thing: ```[self.window addSubview:tabBarController.view];``` – Ben Jan 16 '17 at 00:12
3

I did it this way. I copied this from my App Delegate and it works fine. Basically you add the view controllers to the tab bar then add the tab bar to the window's subview.

Instantiate the instance

iVacationTabBar = [[UITabBarController alloc] init];

However you create the views/view controllers:

 UINavigationController *rvc_tools = [[UINavigationController alloc] initWithRootViewController: vc_tools];
    UINavigationController *rvc_settings = [[UINavigationController alloc] initWithRootViewController: vc_settings];
    UINavigationController *rvc_about = [[UINavigationController alloc] initWithRootViewController: vc_about];
    UINavigationController *rvc_currentpage = [[UINavigationController alloc] initWithRootViewController: vc_currentpage];
    UINavigationController *rvc_backup = [[UINavigationController alloc] initWithRootViewController:vc_backup];

Add the controllers to the array:

NSArray* controllers = [NSArray arrayWithObjects: rvc_currentpage,  rvc_tools,rvc_settings, rvc_backup, rvc_about, nil];

Set the array of view controllers to your tab bar:

[iVacationTabBar setViewControllers: controllers animated:NO];

Add the tab bar to the window's subview.

[_window addSubview:iVacationTabBar.view];
logixologist
  • 3,694
  • 4
  • 28
  • 46
0

You can not add a UITabbarController on a UIViewController. Instead, on a ViewController you have to show the TabbarController, create a TabbarController, set ViewControllers for it, and add it to a Window.

Greg
  • 9,068
  • 6
  • 49
  • 91
Prateek Prem
  • 1,544
  • 11
  • 14