Usage of iOS Tab Bar
It is commonly used to switch between various sub-tasks, views, or models within the same view.
An example of a tab bar is shown below:
Important Properties
- backgroundImage
- items
- selectedItem
Example Code and Steps
Create a new project, select Tabbed Application instead of a single view application, click next, enter the project name, and select create.
Two view controllers and a tab bar are created by default and added to our application.
The AppDelegate.m didFinishLaunchingWithOptions method is as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; self.tabBarController = [[UITabBarController alloc] init]; self.tabBarController.viewControllers = @[viewController1, viewController2]; self.window.rootViewController = self.tabBarController; [self.window makeKeyAndVisible]; return YES; }
Two view controllers are used to assign as the tab bar controller's view controllers.
Run the application to get the following result: