在tabBar区域加个视图

CGRect frame = CGRectMake(0032048);

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图标的办法

tabBar选项卡的背景色如何修改(自定义tabBar)&&推送nav到指定(根)视图控制器_ico

问题:我点击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];