一、使用系統自帶TabBar執行個體化步驟
1、執行個體化相應的VC
RedViewController *red = [[RedViewController alloc] init];
OrangeViewController *orange = [[OrangeViewController alloc] init];
YellowViewController *yellow = [[YellowViewController alloc] init];
GreenViewController *green = [[GreenViewController alloc] init];
BlueViewController *blue = [[BlueViewController alloc] init];
BlackViewController *black = [[BlackViewController alloc] init];
WhiteViewController *white = [[WhiteViewController alloc] init];
2、把VC分别放進導航控制器中
UINavigationController *redNav = [[UINavigationController alloc] initWithRootViewController:red];
UINavigationController *orangeNav = [[UINavigationController alloc] initWithRootViewController:orange];
UINavigationController *yellowNav = [[UINavigationController alloc] initWithRootViewController:yellow];
UINavigationController *greenNav = [[UINavigationController alloc] initWithRootViewController:green];
UINavigationController *blueNav = [[UINavigationController alloc] initWithRootViewController:blue];
UINavigationController *blackNav = [[UINavigationController alloc] initWithRootViewController:black];
UINavigationController *whiteNav = [[UINavigationController alloc] initWithRootViewController:white];
3、把VCrelease掉
[red release];
[orange release];
[yellow release];
[green release];
[blue release];
[black release];
[white release];
4、對每一個Nav進行圖檔和文字的設定
//redNav.navigationItem.title = @"紅";
redNav.title = @"紅"; //設定低欄中标題
//red.title = @"紅"; //可以同時設定導航欄和低欄中的标題
//red.navigationItem.title = @"紅"; //隻是設定導航欄中的标題
//orangeNav.navigationItem.title = @"橙"; //設定導航欄中的标題,但這裡不能顯示,需要到對應的.m檔案中設定,雖然不顯示,但這裡可以擷取導航欄中的标題
orangeNav.title = @"橙";
//yellowNav.navigationItem.title = @"黃";
yellowNav.title = @"黃";
//greenNav.navigationItem.title = @"綠";
greenNav.title = @"綠";
blueNav.title = @"藍";
blackNav.title = @"黑";
white.title = @"白";
特别注意:tabbar或者導覽列上面item的圖,需要透明圖
redNav.tabBarItem.image = [UIImage imageNamed:@"tab_0.png"];
orangeNav.tabBarItem.image = [UIImage imageNamed:@"tab_1.png"];
yellowNav.tabBarItem.image = [UIImage imageNamed:@"tab_2.png"];
greenNav.tabBarItem.image = [UIImage imageNamed:@"tab_3.png"];
blueNav.tabBarItem.image = [UIImage imageNamed:@"tab_s.png"];
blackNav.tabBarItem.image = [UIImage imageNamed:@"tab_0.png"];
whiteNav.tabBarItem.image = [UIImage imageNamed:@"tab_1.png"];
//設定選中圖檔
orangeNav.tabBarItem.selectedImage = [UIImage imageNamed:@"tab_3.png"];
5、把nav們放進一個數組
NSArray *navArr = @[redNav,orangeNav,yellowNav,greenNav,blueNav,blackNav,whiteNav];
[redNav release];
[orangeNav release];
[yellowNav release];
[greenNav release];
[blueNav release];
[blackNav release];
[whiteNav release];
6、執行個體化TabBarController
UITabBarController *tab = [[UITabBarController alloc] init];
特别注意:高度預設49
7、把nav的數組指派給tab
tab.viewControllers = navArr;
8、把tab當做window的root
self.window.rootViewController = tab;
9、tab-1
[tab release];
二、自定義TabBar步驟
#import "MyTabBarViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"
@interface MyTabBarViewController ()
@end
@implementation MyTabBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1、隐藏系統tabbar
self.tabBar.hidden = YES;//self就是這個TabbarVC的對象,這個對象有自己的一個Tabbar
//2、制作tabbar上的VC們
[self makeVC];
//3、制作自定義tabbar
[self makeUI];
// Do any additional setup after loading the view.
}
-(void)makeVC
{
//1、執行個體化VC
FirstViewController *first = [[FirstViewController alloc] init];
SecondViewController *second = [[SecondViewController alloc] init];
ThirdViewController *third = [[ThirdViewController alloc] init];
FourthViewController *fourth = [[FourthViewController alloc] init];
//2、放進導航
UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:first];
UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:second];
UINavigationController *thirdNav = [[UINavigationController alloc] initWithRootViewController:third];
UINavigationController *fourthNav = [[UINavigationController alloc] initWithRootViewController:fourth];
[first release];
[second release];
[third release];
[fourth release];
//3、導航放進數組
NSArray *navArr = @[firstNav,secondNav,thirdNav,fourthNav];
[firstNav release];
[secondNav release];
[thirdNav release];
[fourthNav release];
//4、數組交給self.VCS
self.viewControllers = navArr;
}
-(void)makeUI
{
//背景圖
UIImageView *bar = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-49, self.view.frame.size.width, 49)];
bar.image = [UIImage imageNamed:@"tabbg.png"];
[self.view addSubview:bar];
[bar release];
bar.userInteractionEnabled = YES;
//背景上的按鈕
//算一下,平分之後寬度是多少
float seperateWidth = (self.view.frame.size.width-120)/5;
for(int i =0;i<4;i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(seperateWidth+i*(seperateWidth+30), 2, 30, 30);
[btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%d.png",i]] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnDown:) forControlEvents:UIControlEventTouchUpInside];
[bar addSubview:btn];
btn.tag = 1000+i;
if(i == 0)
{
[btn setImage:[UIImage imageNamed:@"tab_c0.png"] forState:UIControlStateNormal];
}
}
//文字
NSArray *titleArr = @[@"one",@"two",@"three",@"four"];
for(int i =0;i<titleArr.count;i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(seperateWidth+i*(seperateWidth+30), 33, 30, 13)];
label.text = titleArr[i];
label.font = [UIFont systemFontOfSize:12];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
[bar addSubview:label];
[label release];
label.tag = 2000+i;
if(i == 0)
{
label.textColor = [UIColor orangeColor];
}
}
//選中标示(可選)
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(seperateWidth, 47, 30, 1)];
line.backgroundColor = [UIColor orangeColor];
[bar addSubview:line];
[line release];
line.tag = 4000;
}
-(void)btnDown:(UIButton*)btn
{
//1、切換VC
self.selectedIndex = btn.tag - 1000;//selectedIndex就表示了對應下标的那個VC
//2、btn圖檔的切換
for(int i = 0;i<4;i++)
{
UIButton *btn1 = (UIButton*)[self.view viewWithTag:1000+i];
[btn1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%d.png",i]] forState:UIControlStateNormal];
UILabel *lab = (UILabel*)[self.view viewWithTag:2000+i];
lab.textColor = [UIColor whiteColor];
if(lab.tag == btn.tag+1000)
{
lab.textColor = [UIColor orangeColor];
}
}
[btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_c%d.png",btn.tag-1000]] forState:UIControlStateNormal];
UIView *view = (UIView*)[self.view viewWithTag:4000];
view.frame = CGRectMake(btn.frame.origin.x, 47, 30, 1);
}