自定义一个继承于UITabBarController的类,.h文件内容如下:
#import <UIKit/UIKit.h>
@interface CustomTabBar : UITabBarController {
NSMutableArray *buttons;
int currentSelectedIndex;
UIImageView *slideBg;
}
@property (nonatomic,assign) int currentSelectedIndex;
@property (nonatomic,retain) NSMutableArray *buttons;
- (void)hideRealTabBar;
- (void)customTabBar;
- (void)selectedTab:(UIButton *)button;
@end
.m文件内容如下:
#import "CustomTabBar.h"
@implementation CustomTabBar
@synthesize currentSelectedIndex;
@synthesize buttons;
- (void)viewDidAppear:(BOOL)animated{
slideBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottomfocus.png"]];
[self hideRealTabBar];
[self customTabBar];
}
- (void)hideRealTabBar{
for(UIView *view in self.view.subviews){
if([view isKindOfClass:[UITabBar class]]){
view.hidden = YES;
break;
}
}
}
- (void)customTabBar{
UIImageView *imgView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"tabbg2.png"]];
imgView.frame = CGRectMake(0, 425, imgView.image.size.width, imgView.image.size.height);
[self.view addSubview:imgView];
slideBg.frame = CGRectMake(-30, self.tabBar.frame.origin.y, slideBg.image.size.width, slideBg.image.size.height);
//创建按钮
int viewCount = self.viewControllers.count > 5 ? 5 : self.viewControllers.count;
self.buttons = [NSMutableArray arrayWithCapacity:viewCount];
double _width = 320 / viewCount;
double _height = self.tabBar.frame.size.height;
for (int i = 0; i < viewCount; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(i*_width,self.tabBar.frame.origin.y, _width, _height);
[btn addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = i;
[self.buttons addObject:btn];
[self.view addSubview:btn];
[btn release];
}
[self.view addSubview:slideBg];
UIImageView *imgFront = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tabitem.png"]];
imgFront.frame = imgView.frame;
[self.view addSubview:imgFront];
[imgFront release];
[imgView release];
[self selectedTab:[self.buttons objectAtIndex:0]];
}
- (void)selectedTab:(UIButton *)button{
if (self.currentSelectedIndex == button.tag) {
}
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 = CGRectMake(btn.frame.origin.x - 30, btn.frame.origin.y, slideBg.image.size.width, slideBg.image.size.height);
[UIView commitAnimations];
}
- (void) dealloc{
[slideBg release];
[buttons release];
[super dealloc];
}
@end
使用这个类:
// 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];
#import <UIKit/UIKit.h>
@interface CustomTabBar : UITabBarController {
NSMutableArray *buttons;
int currentSelectedIndex;
UIImageView *slideBg;
}
@property (nonatomic,assign) int currentSelectedIndex;
@property (nonatomic,retain) NSMutableArray *buttons;
- (void)hideRealTabBar;
- (void)customTabBar;
- (void)selectedTab:(UIButton *)button;
@end
.m文件内容如下:
#import "CustomTabBar.h"
@implementation CustomTabBar
@synthesize currentSelectedIndex;
@synthesize buttons;
- (void)viewDidAppear:(BOOL)animated{
slideBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottomfocus.png"]];
[self hideRealTabBar];
[self customTabBar];
}
- (void)hideRealTabBar{
for(UIView *view in self.view.subviews){
if([view isKindOfClass:[UITabBar class]]){
view.hidden = YES;
break;
}
}
}
- (void)customTabBar{
UIImageView *imgView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"tabbg2.png"]];
imgView.frame = CGRectMake(0, 425, imgView.image.size.width, imgView.image.size.height);
[self.view addSubview:imgView];
slideBg.frame = CGRectMake(-30, self.tabBar.frame.origin.y, slideBg.image.size.width, slideBg.image.size.height);
//创建按钮
int viewCount = self.viewControllers.count > 5 ? 5 : self.viewControllers.count;
self.buttons = [NSMutableArray arrayWithCapacity:viewCount];
double _width = 320 / viewCount;
double _height = self.tabBar.frame.size.height;
for (int i = 0; i < viewCount; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(i*_width,self.tabBar.frame.origin.y, _width, _height);
[btn addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = i;
[self.buttons addObject:btn];
[self.view addSubview:btn];
[btn release];
}
[self.view addSubview:slideBg];
UIImageView *imgFront = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tabitem.png"]];
imgFront.frame = imgView.frame;
[self.view addSubview:imgFront];
[imgFront release];
[imgView release];
[self selectedTab:[self.buttons objectAtIndex:0]];
}
- (void)selectedTab:(UIButton *)button{
if (self.currentSelectedIndex == button.tag) {
}
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 = CGRectMake(btn.frame.origin.x - 30, btn.frame.origin.y, slideBg.image.size.width, slideBg.image.size.height);
[UIView commitAnimations];
}
- (void) dealloc{
[slideBg release];
[buttons release];
[super dealloc];
}
@end
使用这个类:
// 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];