在tabBar區域加個視圖
CGRect frame = CGRectMake(0,0,320,48);
UIView *v = [[UIView alloc]initWithFrame:frame];
[v setBackgroundColor:[[UIColor allor]initWithRed:70.0/255.0
green:65.0/255.0
blue:62.0/255.0
alpha:1.0]];
[rootController.tabBar insertSubview:v atIndex:0];
[v release];
注意:
addSubview是將view添加到所有層的最頂層,相當於將insertSubView的atIndex參數設置成[view.subviews count]。這是因為子視圖是以棧的方式存放的,每次添加子視圖都是在最后面追加。insertSubview是添加到指定的層位置。
[view addSubview:one view] 等價於[view insertSubview:oneview atIndex:[view.subviews count]];
自定義透明tabBar
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
FirstViewController *mainViewController = [[FirstViewController alloc] init];
SecondViewController *searchViewController = [[SecondViewController alloc]init];
ThirdViewController *myselfViewController = [[ThirdViewController alloc]init];
ForthViewController *settingViewController = [[ForthViewController alloc]init];
//隱藏tabbar所留下的黑邊(試着注釋后你會知道這個的作用)
mainViewController.hidesBottomBarWhenPushed = true;
searchViewController.hidesBottomBarWhenPushed = true;
myselfViewController.hidesBottomBarWhenPushed = true;
settingViewController.hidesBottomBarWhenPushed = true;
mainViewController.title = @"首頁";
searchViewController.title = @"搜索";
myselfViewController.title = @"我";
settingViewController.title = @"設置";
//創建導航
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainViewController ];
UINavigationController *nav1 = [[ UINavigationController alloc] initWithRootViewController:searchViewController];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:myselfViewController];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:settingViewController];
//創建數組
NSMutableArray *controllers = [[NSMutableArray alloc]init];
[controllers addObject:nav];
[controllers addObject:nav1];
[controllers addObject:nav2];
[controllers addObject:nav3];
//創建tabbar
tabBarController = [[ CustomTabBar alloc] init];
tabBarController.viewControllers = controllers;
tabBarController.selectedIndex = 0;
//顯示
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
附:
改變tabbar圖標的辦法
問題:我點擊click me,會push到下一個頁面,如果用的是系統的tabbar,單擊首頁的時候會pop到首頁,但是用自定義的無法實現,求解決方案.
解決:自行替換下面的方法,其實也就是加了一句話
- (void)selectedTab:(UIButton *)button{
if (self.currentSelectedIndex == button.tag) {
//判斷是否是UINavigation類
if([[self.viewControllers objectAtIndex:button.tag]isKindOfClass:[UINavigationController class]])
//返回根視圖控制器
[[self.viewControllers objectAtIndex:button.tag] popToRootViewControllerAnimated:YES];
}
self.currentSelectedIndex = button.tag;
self.selectedIndex = self.currentSelectedIndex;
[self performSelector:@selector(slideTabBg:) withObject:button];
}
#import "Ivan_UITabBar.h"
@implementation Ivan_UITabBar
@synthesize currentSelectedIndex;
@synthesize buttons;
- (void)viewDidAppear:(BOOL)animated{
slideBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"slide"]];
[self hideRealTabBar];
[self customTabBar];
}
- (void)hideRealTabBar{
for(UIView *view in self.view.subviews){
if([view isKindOfClass:[UITabBar class]]){
view.hidden = YES;
break;
}
}
}
- (void)customTabBar{
UIView *tabBarBackGroundView = [[UIView alloc] initWithFrame:self.tabBar.frame];
tabBarBackGroundView.backgroundColor = [UIColor grayColor];
//創建按鈕
int viewCount = self.viewControllers.count > 5 ? 5 : self.viewControllers.count;
self.buttons = [NSMutableArray arrayWithCapacity:viewCount];
double _width = 320 / viewCount;
// NSLog(@"%d",viewCount);
double _height = self.tabBar.frame.size.height;
for (int i = 0; i < viewCount; i++) {
UIViewController *v = [self.viewControllers objectAtIndex:i];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(i*_width, 0, _width, _height);
[btn addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = i;
// NSLog(@"%@",v.tabBarItem.image);
[btn setBackgroundImage:v.tabBarItem.image forState:UIControlStateNormal];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _height-20, _width, _height-30)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.text = v.tabBarItem.title;
titleLabel.textAlignment = 1;
titleLabel.textColor = [UIColor whiteColor];
[btn addSubview:titleLabel];
[titleLabel release];
[self.buttons addObject:btn];
[tabBarBackGroundView addSubview:btn];
}
[self.view addSubview:tabBarBackGroundView];
[tabBarBackGroundView addSubview:slideBg];
[self.view insertSubview:tabBarBackGroundView atIndex:0];
[tabBarBackGroundView release];
[self selectedTab:[self.buttons objectAtIndex:0]];
}
- (void)selectedTab:(UIButton *)button{
if (self.currentSelectedIndex == button.tag) {
[[self.viewControllers objectAtIndex:button.tag] popToRootViewControllerAnimated:YES];
}
self.currentSelectedIndex = button.tag;
self.selectedIndex = self.currentSelectedIndex;
[self performSelector:@selector(slideTabBg:) withObject:button];
}
- (void)slideTabBg:(UIButton *)btn{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.20];
[UIView setAnimationDelegate:self];
slideBg.frame = btn.frame;
[UIView commitAnimations];
}
UITabBarController注意的地方
如果你的程序界面如上圖所示的這個樣子,那么你一定是同時使用了UITabBarController以及UINavigationController吧。
再如果,你希望實現這樣子的效果:
1。用戶起初在“分類”這個視圖里面查看了一些菜系,
2。然后他點擊了“搜索”Tab,又在里面看了一些搜索出來的菜譜,
3。再然后當他准備回到“分類”這個視圖的時候,事實上用戶心里是希望回到“分類”這個視圖的根視圖,在那里重新選擇分類,進入分類再看菜譜。但是iPhone默認是記憶用戶之前在第一步里面的那個視圖的,不會自動回到根視圖。
解決辦法是將這個
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
viewController popToRootViewControllerAnimated:YES];
}
添加到AppDelegate.m文件中。一般人都是這么做的。
但是!
其實在這里隱藏着一個問題,如果如圖所示你的4個TabBar都是指向4個NavigationController,那么沒有問題,運行OK。
但如果你的4個TabBar有任何一個指向的不是NavigationController,那么程序就會crash。因為非NavigationController不能夠響應 popToRootViewControllerAnimated: 方法。
我的第4個TabBar指向的是一個ViewController,程序運行之后一點擊第4個TabBar就崩潰,自己也只是在ViewController和IB里面仔細找錯誤,浪費了很多時間。
下面貼出更加安全的方法,把上面的那段代碼改成這樣子:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[UINavigationController class]]) {
[(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
}
}
就好了。
//推送到指定視圖控制器
XXView *rootViewController = nil;
for (UIViewController *VC in self.navigationController.viewControllers)
{
if ([VC isKindOfClass:[XXView class]]) {
rootViewController = (XXView *)VC;
}
}
[self.navigationController popToViewController:rootViewController animated:YES];