整个自定义TabBarController实现自主风格

 要自定义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;
 4int 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{
15for(UIView *view in self.view.subviews){
16if([view isKindOfClass:[UITabBar class]]){
17             view.hidden = YES;
18break;
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//创建按钮
30int viewCount = self.viewControllers.count > 5 ? 5 : self.viewControllers.count;
31     self.buttons = [NSMutableArray arrayWithCapacity:viewCount];
32double _width = 320 / viewCount;
33double _height = self.tabBar.frame.size.height;
34for (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{
54if (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];

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值