Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Last active December 14, 2015 02:29
Show Gist options
  • Select an option

  • Save lamprosg/5013598 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/5013598 to your computer and use it in GitHub Desktop.
(iOS) Creating a tab bar
//Create for every single view you're going to have a new view controller class
//Subclass of UIViewController
//With a correspoding .xib
//.
//Creating outlet for the root controller (which will be a tab bar) in the delegate .h file
@property (strong, nonatomic) IBOutlet UITabBarController *tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
/*****************************************************************************/
//We’re using a controller class provided by Apple (UITabBarController) instead of one we wrote ourselves.
//See header file
//Show the TabBarController.xib
[[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil];
//Set the root controller to the outlet we set pointing to the Tab bar controller (UITabBarController)
self.window.rootViewController = self.tabBarController;
/*****************************************************************************/
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
//Then create an empty .xib (named after the one in loadNibNamed:) and add a view controller object
//Change the File's Owner class of the xib to the delegate's one (ex. AppDelegate)
//At the connection inspector find the outlet we created in the delegate's .h file and connect it
//to the tab bar Controller
//.
//Add as many tab bar items you want to the tab bar.
//Then change every controller of each tab bar the NIB Name and the Class to the one you're going
//to show.
//.
//Select every tab bar item and customize it as you wish
//Your tab bar is ready
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
tabBarController = [[UITabBarController alloc] init];
MyViewController* vc1 = [[MyViewController alloc] init];
MyOtherViewController* vc2 = [[MyOtherViewController 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];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment