Programmatically creating a tab bar for ViewController

I was looking at programmatically adding a tab bar to my view controller, because, having a scroll view, I cannot place it without being in the middle of my view. I don’t understand how to add it. Do I need to initiate it in my ViewDidLoad method or in my AppDelegate ? If I have:

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

How can I highlight it at the bottom of my scrollview ?

Thanks!

Now in the 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 is crashing and not sure if this is a release that I am missing.

Edit: For someone else, this is interesting in Appdelegate.m:

 self.tabBarController = [[[UITabBarController alloc] init] autorelease]; self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4]; 
+6
source share
3 answers

Take a look at this documentation provided by Apple: ViewControllers .

Code example:

 - (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 current view as a subview of the window [window addSubview:tabBarController.view]; } 
+7
source

I did it like that. I copied this from my App Delegate and it works great. Basically, you add view controllers to the tab bar, and then add the tab bar to the window subtitle.

Create Instance Instance

 iVacationTabBar = [[UITabBarController alloc] init]; 

However, you create 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 controllers to the array:

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

Define an array of view controllers in the tab bar:

 [iVacationTabBar setViewControllers: controllers animated:NO]; 

Add a tab bar to the window subzone.

 [_window addSubview:iVacationTabBar.view]; 
+3
source

You cannot add a UITabbarController to a UIViewController . Instead, on the ViewController you should show the TabbarController , create a TabbarController , set the ViewController for it and add it to the Window .

0
source

Source: https://habr.com/ru/post/947011/


All Articles