How to customize the tab bar using iOS 5 appearance API

Source: Internet
Author: User

Http://ios-blog.co.uk/tutorials/how-to-customize-the-tab-bar-using-ios-5-appearance-api/

Great app design is now a must-have when developing apps for the app store. especially, now that you have to contend with so far other PAS for attention. if your apps are not selling as well as they shocould spice it up with a good design and you shocould see
An uplift.

In this post, I am going to show you how to design the tab bar you see in the screenshot below. the tab bar has a leather background with a black selection background. there are also four icons that are specific to a music app.

We shall use some sample iPhone app design files and at the end of this tutorial, I will share a link to the sample project with the design Images and icons so you can use them in your
Apps for free.

In previous ios sdk's, it was a pain to totally customise the tabbar in this way. in ios5, Apple has recognized the need to be able to customise the UI so they have added the appearance API thankfully. this gives developers more control on the look and feel
Of the app.

Starting with a simple user interface.

Well, to start. fire up xcode, and create a new sample project. give it any name you like and choose the Single View Project. we are going to code up our tab views manually. just like the pros.

In the appdelegate. M file, add the following code. in that piece of code, we are creating four views and adding them to a tab controller. these views are empty for now because we don't need any content in them for the purposes of this project.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    
    UITabBarController *tabController = [[UITabBarController alloc] init];
    
    UIViewController *viewController1 = [[UIViewController alloc] init];
    
    UIViewController *viewController2 = [[UIViewController alloc] init];
    
    UIViewController *viewController3 = [[UIViewController alloc] init];
    
    UIViewController *viewController4 = [[UIViewController alloc] init];
    
    
    tabController.viewControllers = [NSArray arrayWithObjects:viewController1, 
                                     viewController2, 
                                     viewController3, 
                                     viewController4, nil];
    
    
    self.window.rootViewController = tabController;
    
    [self.window makeKeyAndVisible];
    return YES;
}

If you run the application in the simulator, you shoshould see something similar to the image below. Kind of boring, I know but we will get there.

Adding tabs to the tab Controller

In the design resources folder, I have four icons. to add them to our tabs, we will need to add them to our project. when they are added, note down the names of the images. in our case, they are called artist-tab.png, music-tab.png, podcast-tab.png and clock-tab.png.

We will now create uitabbaritems using the following code.

UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Artists" image:[UIImage imageNamed:@"artist-tab.png"] tag:1];

This will initialise A uitabbaritem with the image and title given. In this case, the image is the specified artist-tab.png "and the title is called" artists ".

The next step is to set this as the tab item for one of our viewcontrollers.

[viewController1 setTabBarItem:tab1];

This is how our application: didfinishlaunchingwitexceptions method looks like.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    
    UITabBarController *tabController = [[UITabBarController alloc] init];
    
   
    
    UIViewController *viewController1 = [[UIViewController alloc] init];
    
    UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Artists" image:[UIImage imageNamed:@"artist-tab.png"] tag:1];
    [viewController1 setTabBarItem:tab1];
    
    UIViewController *viewController2 = [[UIViewController alloc] init];
    
    UIViewController *viewController3 = [[UIViewController alloc] init];
    
    UIViewController *viewController4 = [[UIViewController alloc] init];
    
    
    tabController.viewControllers = [NSArray arrayWithObjects:viewController1, 
                                     viewController2, 
                                     viewController3, 
                                     viewController4, nil];
    
    
    self.window.rootViewController = tabController;
    
    [self.window makeKeyAndVisible];
    return YES;
}

If you run this in the simulator, you should see the something similar to the image below.

Our little app is taking some shape. The next step is to extrapoate the method and add tabs for all our views.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    
    UITabBarController *tabController = [[UITabBarController alloc] init];

    UIViewController *viewController1 = [[UIViewController alloc] init];
    
    UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Artists" 
                                                       image:[UIImage imageNamed:@"artist-tab.png"] tag:1];
    [viewController1 setTabBarItem:tab1];
    
    
    UIViewController *viewController2 = [[UIViewController alloc] init];
    UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:@"Music" 
                                                       image:[UIImage imageNamed:@"music-tab.png"] tag:2];
    [viewController2 setTabBarItem:tab2];
    
    UIViewController *viewController3 = [[UIViewController alloc] init];
    UITabBarItem *tab3 = [[UITabBarItem alloc] initWithTitle:@"Podcast" 
                                                       image:[UIImage imageNamed:@"podcast-tab.png"] tag:3];
    [viewController3 setTabBarItem:tab3];
    
    UIViewController *viewController4 = [[UIViewController alloc] init];
    UITabBarItem *tab4 = [[UITabBarItem alloc] initWithTitle:@"Time" 
                                                       image:[UIImage imageNamed:@"clock-tab.png"] tag:4];
    [viewController4 setTabBarItem:tab4];    
    
    tabController.viewControllers = [NSArray arrayWithObjects:viewController1, 
                                     viewController2, 
                                     viewController3, 
                                     viewController4, nil];
    
    
    self.window.rootViewController = tabController;
    
    [self.window makeKeyAndVisible];
    return YES;
}

This will add the other tabs to our tab controller, using our four icon images. This is what you should see when running the app in the simulator

Styling the tab bar using the iOS 5 appearance API

This is starting to look good. We just need to add our leather background. To do this, we will create a new method in the appdelegate. M file and call it customiseappearance.

In this method, we will add the background to the tab bar .. see the code below.

- (void)customizeInterface
{
    UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
}

And then make sure to call the method at the top of our application: didfinishlaunchingwitexceptions
Method. This will customize all the tab bars in our app globally by adding an image
Called tabbar.png. The image can be downloaded as part of the sample project of this post.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self customizeInterface];

Run the application and you will see that our tab bar has a nice leather background to it.

The Last Thing To Do It customise how the tab bar item will look when the user taps on each item.
We employ the appearance API again to make this happen.

- (void)customizeInterface
{
    UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
    
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"selection-tab.png"]];
}

In the code above, we have added the third line, which sets the selection indicator of our tabs.
This is the final step and you shoshould see the following when you run the app.

There you have it. A fully customized tab controller with a leather background. Now you can go forth and modify your apps, make the look good and have more.



Download the sample project here

Download the sample project here

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.