今天雨松MOMO给大家讲讲如何在Three20中自定义TabBar,如下图所示,IOS源生的TabBar中的图片必需是这种无彩图,如果添加有色图片时将无法显示。 如果要学习源生的TabBar的话大家可以参考我之前的文章: Three20研究院之TabBar与下拉列表访问数据与刷新(五)
下面我们学习如何在Three20中自定义TabBar。代码其实非常非常简单,就是在界面绘制几个自定义按钮,然后当点击按钮后切换TabBar的item。如下图所示,雨松MOMO写了一个简单的例子,“历史”与“主页”按钮,点击后将使用TabBar切换item进入各自的ViewController。
下面就直接上代码,大家仔细看看应该都能懂,核心的代码其实就几行。
AppDelegate.m 这个类没什么说的。
01 | #import "AppDelegate.h" |
03 | @implementation AppDelegate |
11 | - ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
14 | [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; |
17 | TTNavigator* navigator = [TTNavigator navigator]; |
18 | navigator.persistenceMode = TTNavigatorPersistenceModeAll; |
19 | navigator.window = [[[UIWindow alloc] initWithFrame:TTScreenBounds()] autorelease]; |
23 | TTURLMap* map = navigator.URLMap; |
26 | [map from:@ "*" toViewController:[TTWebController class ]]; |
28 | [map from:@ "tt://Tab" toModalViewController:[RootViewController class ]];//入口 |
29 | [map from:@ "tt://Menu" toModalViewController:[MenuViewController class ]];// |
30 | [map from:@ "tt://Test" toModalViewController:[TestViewController class ]];// |
32 | if (![navigator restoreViewControllers]) |
35 | [navigator openURLAction:[TTURLAction actionWithURLPath:@ "tt://Tab" ]]; |
RootViewController.h
1 | #import <Foundation/Foundation.h> |
2 | #import <Three20/Three20.h> |
5 | #import "Three20UI/Three20UI+Additions.h" |
7 | @interface RootViewController : UITabBarController |
RootViewController.m
01 | #import "RootViewController.h" |
03 | @interface RootViewController () |
07 | @implementation RootViewController |
09 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil |
11 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; |
23 | [self setTabURLs:[NSArray arrayWithObjects:@ "tt://Menu" , |
28 | self.tabBar.hidden = YES; |
31 | UIButton *test0 = [UIButton buttonWithType:UIButtonTypeCustom]; |
32 | [test0 setFrame: CGRectMake(15, 200, 256, 49)]; |
36 | [test0 addTarget:self action:@selector(testPressed:) forControlEvents:UIControlEventTouchDown]; |
37 | [self.view addSubview:test0]; |
39 | UIButton *test1 = [UIButton buttonWithType:UIButtonTypeCustom]; |
40 | [test1 setFrame: CGRectMake(15, 250, 256, 49)]; |
44 | [test1 addTarget:self action:@selector(testPressed:) forControlEvents:UIControlEventTouchDown]; |
45 | [self.view addSubview:test1]; |
49 | -( void )testPressed:(id)buttonID |
52 | UIButton *button = (UIButton *)buttonID; |
57 | [self setSelectedIndex:button.tag]; |
61 | -( void )viewWillAppear:( BOOL )animated |
63 | [super viewWillAppear:animated]; |
64 | [self.navigationController setNavigationBarHidden:YES animated:NO]; |
MenuViewController.m
01 | #import "MenuViewController.h" |
03 | @interface MenuViewController () |
07 | @implementation MenuViewController |
13 | UIImageView *title = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 120, 120)]; |
17 | [self.view addSubview:title]; |
21 | UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(150, 0, 120, 120)]; |
22 | label.text = @ "大家好,我是若若娃娃!" ; |
23 | [label setBackgroundColor: [UIColor blackColor]]; |
24 | label.textColor = [UIColor whiteColor]; |
25 | label.lineBreakMode = UILineBreakModeWordWrap; |
26 | label.numberOfLines = 0; |
27 | [self.view addSubview:label]; |
31 | -( void )viewWillAppear:( BOOL )animated |
33 | [super viewWillAppear:animated]; |
34 | [self.navigationController setNavigationBarHidden:YES animated:NO]; |
TestViewController.m
01 | #import "TestViewController.h" |
03 | @interface TestViewController () |
07 | @implementation TestViewController |
13 | UIImageView *title = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 120, 120)]; |
17 | [self.view addSubview:title]; |
21 | UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(150, 0, 120, 120)]; |
22 | label.text = @ "大家好,我是雨松MOMO!" ; |
23 | [label setBackgroundColor: [UIColor blackColor]]; |
24 | label.textColor = [UIColor whiteColor]; |
25 | label.lineBreakMode = UILineBreakModeWordWrap; |
26 | label.numberOfLines = 0; |
27 | [self.view addSubview:label]; |
32 | -( void )viewWillAppear:( BOOL )animated |
34 | [super viewWillAppear:animated]; |
35 | [self.navigationController setNavigationBarHidden:YES animated:NO]; |
怎么样?挺较简单的吧,嘿嘿!因为比较简单,源码工程我就不放出了,大家仔细看看博文就应该能理解。写完博客趁着家里人睡着了MOMO得开始玩暗黑3啦,嘿嘿。在家里比较受限制,干什么家人都会问一句为什么,很不爽啊。。
上述的方法如果在子viewcontroller中是会被挡住的。如果彻底的想把tabBar隐藏可以使用如下的方法。
01 | - ( void )makeTabBarHidden:( BOOL )hide |
03 | if ( [self.view.subviews count] < 2 ) |
08 | if ( [[self.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class ]] ) |
09 | contentView = [self.view.subviews objectAtIndex:1]; |
11 | contentView = [self.view.subviews objectAtIndex:0]; |
15 | contentView.frame = self.view.bounds; |
19 | contentView.frame = CGRectMake(self.view.bounds.origin.x, |
20 | self.view.bounds.origin.y, |
21 | self.view.bounds.size.width, |
22 | self.view.bounds.size.height - self.tabBar.frame.size.height); |
25 | self.tabBar.hidden = hide; |