How To Customize The Tab Bar Using iOS 5 Appearance API

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 many other pas for attention. If your apps are not selling as well as they should spice it up with a good design and you should 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 recognised 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 with. 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 should 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 “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:DidFinishLaunchingWithOptions 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 extrapolate 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:DidFinishLaunchingWithOptions
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 should 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 sell more.



Download the sample project here


Download the sample project here


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To add custom functions to the NX menu, you can use the NXOpen.MenuBar class in NXOpen.NET API. Here is an example code that demonstrates how to add a custom function to the NX menu: ```vb Imports System Imports NXOpen Module Module1 Sub Main() ' Get the NX session Dim theSession As Session = Session.GetSession() ' Get the UI work part Dim theUI As UI = theSession.UI Dim lw As ListingWindow = theSession.ListingWindow ' Get the menu bar Dim menuBar As MenuBar = theUI.MenuBar ' Get the File menu Dim fileMenu As Menu = menuBar.GetMenu("File") ' Add a separator to the File menu fileMenu.AddSeparator() ' Add a custom function to the File menu Dim menuItem As MenuItem = fileMenu.AddMenuItem("Custom Function", AddressOf CustomFunction) ' Show a message box when the custom function is clicked Sub CustomFunction(ByVal item As MenuItem) lw.Open() lw.WriteLine("Custom function is clicked!") lw.Close() End Sub ' Start the NX message loop to display the menu theUI.NXMessageBox.Show("Menu Example", NXMessageBox.DialogType.Information, "Click OK to display the menu") theUI.NXMessageBox.GetMessage() ' Remove the custom function from the menu fileMenu.RemoveMenuItem(menuItem) End Sub End Module ``` In the above code, we first obtain the NX session and UI work part. Then, we get the MenuBar object using `theUI.MenuBar`. Next, we retrieve the desired menu (e.g., "File") using `GetMenu()` method. We can add a separator using `AddSeparator()` method and add a custom function using `AddMenuItem()` method, specifying the function to be called when the menu item is clicked. In the example above, the `CustomFunction` is a sub that will be executed when the custom function menu item is clicked. You can customize the behavior of this function to perform your desired actions. After adding the custom function, we start the NX message loop using `theUI.NXMessageBox.Show()` and `theUI.NXMessageBox.GetMessage()` to display the menu. Finally, we remove the custom function from the menu using `RemoveMenuItem()` method. Please note that above code is just an example, and you may need to adjust it based on your specific requirements and menu structure in NX.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值