整個自定義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;
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];





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM