整个自定义TabBarController实现自主风格
2012-01-04 15:45 张智清 阅读(10075) 评论(0) 编辑 收藏 举报要自定义UITabBarController,首先我们必须了解UITabBarController结构与其各个相关类的关系(TabBarController、TabBar、TabButton及ViewController)。其中,TabButton是一个双态的Button(选中和未选中),行为和CheckBox、RadioButton类似。TabBar是TabButton的容器,负责TabButton的排布和互斥,保证同时只有一个Button为选中态。TabBarController包含了TabBar,并管理这一个ViewController的栈,在TabBar上的按钮点击时对栈上的ViewController位置进行相应的调整,从而保持TabBar和ViewController栈之间的一致性。
熟悉了基本结构后,一般有以下常见的方案来完整自定义TabBarController。
方案一:替换部分现有的实现
目标是替换掉系统的部分控件,可通过隐藏TabBar的方式来实现。只要做到隐藏原有的TabBar后,不要忘了在新定制的TabBar的当前选中按钮变化时通过UITabBarController的setSelectedIndex接口设置ViewController栈的当前ViewController即可。
View Code
1 #import <UIKit/UIKit.h>
2 @interface CustomTabBar : UITabBarController {
3 NSMutableArray *buttons;
4 int currentSelectedIndex;
5 UIImageView *slideBg;
6 }
7
8 @property (nonatomic,assign) int currentSelectedIndex;
9 @property (nonatomic,retain) NSMutableArray *buttons;
10
11 - (void)hideRealTabBar;
12 - (void)customTabBar;
13 - (void)selectedTab:(UIButton *)button;
14
15 @end
View Code
1 #import "CustomTabBar.h"
2
3 @implementation CustomTabBar
4
5 @synthesize currentSelectedIndex;
6 @synthesize buttons;
7
8 - (void)viewDidAppear:(BOOL)animated{
9 slideBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottomfocus.png"]];
10 [self hideRealTabBar];
11 [self customTabBar];
12 }
13
14 - (void)hideRealTabBar{
15 for(UIView *view in self.view.subviews){
16 if([view isKindOfClass:[UITabBar class]]){
17 view.hidden = YES;
18 break;
19 }
20 }
21 }
22
23 - (void)customTabBar{
24 UIImageView *imgView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"tabbg2.png"]];
25 imgView.frame = CGRectMake(0, 425, imgView.image.size.width, imgView.image.size.height);
26 [self.view addSubview:imgView];
27 slideBg.frame = CGRectMake(-30, self.tabBar.frame.origin.y, slideBg.image.size.width, slideBg.image.size.height);
28
29 //创建按钮
30 int viewCount = self.viewControllers.count > 5 ? 5 : self.viewControllers.count;
31 self.buttons = [NSMutableArray arrayWithCapacity:viewCount];
32 double _width = 320 / viewCount;
33 double _height = self.tabBar.frame.size.height;
34 for (int i = 0; i < viewCount; i++) {
35 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
36 btn.frame = CGRectMake(i*_width,self.tabBar.frame.origin.y, _width, _height);
37 [btn addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
38 btn.tag = i;
39 [self.buttons addObject:btn];
40 [self.view addSubview:btn];
41 [btn release];
42 }
43 [self.view addSubview:slideBg];
44 UIImageView *imgFront = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tabitem.png"]];
45 imgFront.frame = imgView.frame;
46 [self.view addSubview:imgFront];
47 [imgFront release];
48 [imgView release];
49 [self selectedTab:[self.buttons objectAtIndex:0]];
50
51 }
52
53 - (void)selectedTab:(UIButton *)button{
54 if (self.currentSelectedIndex == button.tag) {
55
56 }
57 self.currentSelectedIndex = button.tag;
58 self.selectedIndex = self.currentSelectedIndex;
59 [self performSelector:@selector(slideTabBg:) withObject:button];
60 }
61
62 - (void)slideTabBg:(UIButton *)btn{
63 [UIView beginAnimations:nil context:nil];
64 [UIView setAnimationDuration:0.20];
65 [UIView setAnimationDelegate:self];
66 slideBg.frame = CGRectMake(btn.frame.origin.x - 30, btn.frame.origin.y, slideBg.image.size.width, slideBg.image.size.height);
67 [UIView commitAnimations];
68 }
69
70 - (void) dealloc{
71 [slideBg release];
72 [buttons release];
73 [super dealloc];
74 }
75 @end
使用这个自定义类来创建UITabBarController
View Code
1 // Override point for customization after application launch.
2 FirstViewController *mainViewController = [[FirstViewController alloc] init];
3 SecondViewController *searchViewController = [[SecondViewController alloc]init];
4 ThirdViewController *myselfViewController = [[ThirdViewController alloc]init];
5 ForthViewController *settingViewController = [[ForthViewController alloc]init];
6
7 //隐藏tabbar所留下的黑边(试着注释后你会知道这个的作用)
8 mainViewController.hidesBottomBarWhenPushed = true;
9 searchViewController.hidesBottomBarWhenPushed = true;
10 myselfViewController.hidesBottomBarWhenPushed = true;
11 settingViewController.hidesBottomBarWhenPushed = true;
12
13 mainViewController.title = @"首页";
14 searchViewController.title = @"搜索";
15 myselfViewController.title = @"我";
16 settingViewController.title = @"设置";
17
18 //创建导航
19 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainViewController ];
20 UINavigationController *nav1 = [[ UINavigationController alloc] initWithRootViewController:searchViewController];
21 UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:myselfViewController];
22 UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:settingViewController];
23 //创建数组
24 NSMutableArray *controllers = [[NSMutableArray alloc]init];
25 [controllers addObject:nav];
26 [controllers addObject:nav1];
27 [controllers addObject:nav2];
28 [controllers addObject:nav3];
29
30 //创建tabbar
31 tabBarController = [[ CustomTabBar alloc] init];
32 tabBarController.viewControllers = controllers;
33 tabBarController.selectedIndex = 0;
34
35 //显示
36 [self.window addSubview:tabBarController.view];
37 [self.window makeKeyAndVisible];