MainBarViewController *mainVeiw = [[MainBarViewController alloc] init];
self.window.rootViewController = mainVeiw;
“MainBarViewController.h”
@interface MainBarViewController : UITabBarController{
UIImageView *selectView;
}
“MainBarViewController.m”
@implementation MainBarViewController
– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.tabBar.hidden = YES;//隐藏系统TabBar
}
return self;
}
//装载视图控制器
-(void)loadViewControllers{
OneViewController *oneView = [[OneViewController alloc] init];
oneView.title = @”one”;
UINavigationController *oneNav = [[UINavigationController alloc] initWithRootViewController:oneView];
TwoViewController *twoView = [[TwoViewController alloc] init];
twoView.title = @”two”;
UINavigationController *twoNav = [[UINavigationController alloc] initWithRootViewController:twoView];
NSArray *viewControllers = @[oneNav,twoNav];
[self setViewControllers:viewControllers];
}
-(void)loadCustomTabBarView{
/*
*层次:背景(最下)、选中图片(中间)、按钮(最上)
*/
//初始化自定义TabBar背景
UIImageView *tabBarGB = [[UIImageView alloc] initWithFrame:CGRectMake(0, 431, 320, 49)];
tabBarGB.userInteractionEnabled = YES;
tabBarGB.image = [UIImage imageNamed:@”barbg.png”];
[self.view addSubview:tabBarGB];
//初始化自定义选中背景
selectView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 49.0/2–45.0/2, 53, 45)];
selectView.image = [UIImage imageNamed:@”select.png”];
[tabBarGB addSubview:selectView];
//初始化自定义TabBarItem -> UIButton
float coordinateX = 0;
for (int index=0; index<2; index++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = index;
button.frame = CGRectMake(14+coordinateX, 49.0/2–20, 42, 40);
NSString *imageName = [NSString stringWithFormat:@”%d.png”,index+1];//按钮图片
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[tabBarGB addSubview:button];
[button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
coordinateX += 62;
}
}
-(void)changeViewController:(UIButton *)button{
self.selectedIndex = button.tag;
[UIView beginAnimations:nil context:NULL];//动画
selectView.frame = CGRectMake(10+button.tag*62, 49.0/2–45.0/2, 53, 45);
[UIView commitAnimations];
}
– (void)viewDidLoad
{
[super viewDidLoad];
[self loadViewControllers];
[self loadCustomTabBarView];
}
demo下载地址:http://www.kuaipan.cn/file/id_82777686379857034.htm