How To Customise the Tab Bar (UITabBar) in an iPhone Application (Part 1 of 2)

转载自:http://www.rumex.it/2010/07/how-to-customise-the-tab-bar-uitabbar-in-an-iphone-application-part-1-of-2/

 

Just as we were about to go live with iOrlando, it became clear that due to some requirements from our sponsor a standard UITabBar would not work for our needs.  The sponsor wanted to use just their logo for their tab and not have a text label.

Unfortunately the standard UITabBar controller is quite limited in terms of the styling.  I know this is deliberate on apples part to provide uniformity of interface but that doesn’t always work as this example proves.

So, creating a custom nav bar.  I found surprisingly little information on the internet on how to achieve this.  I was clear that I would need to subclass UITabBarController but many of the solutions involved using private APIs and I needed to avoid this as I needed to get my apple approved for sale on the AppStore.

The basis of my approach is to create a “fake” tab bar that sits on top of the old tab bar and replicated it’s functionality.

A demo of part’s 1 and 2 of the tutorial is available here: https://github.com/rumex/RXCustomTabBar

Before we start I know Apple’s guide for this control says “Not Intended for Subclassing”.  This article is purely the approach I took, I am not claiming it is the best to do it, I am just sharing what I did.  What I can say is that it is the method I used in iOrlando Tourist Map and it has been approved by Apple with no issues through a number of updates.

For the sake of this demo I am going to use the navigation for iOrlando.  The basic concept of my approach was:

  1. Create the UITabBar as normal adding an item for each view you require.
  2. Hide the existing Tab Bar Items
  3. Add my own items (custom buttons designed to mimic the TabBar)
  4. Create the functionality to make the buttons perform the expected actions.

1. Create the Tab Bar Based Application as Normal
This tutorial starts with an application that already has a Tab Bar in use (with four items in my case).  If you don’t know how to get to this point I have created another tutorial detailing how to create a basic Tab Bar application and would recommend you read that first.  You can find that at:http://www.rumexit.co.uk/2010/07/tutorial-how-to-create-an-iphone-application-with-a-tab-bar/

2. Hide the Existing Tab Bar Items
OK, now we have a working Tab Bar application we need to subclass UITabBarController.  Right click in Xcode and Add a new file.

Select a new Objective-C Class and make it a subclass of NSObject.

Give it a name (I have called mine CustomTabBar).

We are going to edit CustomTabBar.h so select it and you will see the current contents of the file.  Since we are subclassing UITabBarController we need to change NSObject to UITabBarController.  I am also going to declare a method called hideExistingTabBar. My CustomTabBar.h now looks like this:

1
2
3
4
5
6
@interface CustomTabBar : UITabBarController {
 
}
 
-( void ) hideExistingTabBar;
@end

Now I am going to edit CustomTabBar.m.  We are going to create our new hideExistingTabBar method and override viewDidAppear (to call hideExistingTabBar) and dealloc to take care of our garbage collection later on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#import "CustomTabBar.h"
 
@implementation CustomTabBar
 
- ( void )viewDidAppear:( BOOL )animated {
     [ super viewWillAppear:animated];
 
     [ self hideExistingTabBar];
}
 
- ( void )hideExistingTabBar
{
     for (UIView *view in self .view.subviews)
     {
         if ([view isKindOfClass:[UITabBar class ]])
         {
             view.hidden = YES ;
             break ;
         }
     }
}
 
- ( void )dealloc {
     [ super dealloc];
}

You will see our hideExistingTabBar method is iterating through every subview in the view.

1
for (UIView *view in self .view.subviews)

Checking to see if it is a TabBar.

1
if ([view isKindOfClass:[UITabBar class ]])

If it is a tab bar then it is hiding it.

1
view.hidden = YES ;

We have now created a subclass of UITabBarController that at runtime will hide the existing Tab Bar items. Now all we need to do is to change our existing TabBar to use our custom controller instead of the standard one.

Make sure you CustomTabBar.h and .m are saved and then double click on MainWindow.xib again to open up Interface Builder.

Select the tab bar controller.  Then in the identity inspector change the Class to CustomTabBar.

Now save and close interface builder.

You can now run your project in the simulator and you will see Tab Bar has completely disappeared.  Clearly this is not very useful as you can’t access any other views but it is a good start as we are now ready to add our custom buttons.

Part 2 can be found here: http://www.rumexit.co.uk/2010/11/how-to-customise-the-tab-bar-uitabbar-in-an-iphone-application-part-2-of-2/#more-143

转载于:https://www.cnblogs.com/liuboo/archive/2012/10/19/2731182.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值