//.h 文件
//成员变量定义:
@interface ZAAppDelegate : UIResponder <UIApplicationDelegate>
{
UIView *tabbarView;
UINavigationController *firstNV;
UINavigationController *secondNV;
UINavigationController *thirdNV;
}
//.m 文件
#import "ZAAppDelegate.h"
//此处的ZAFirstViewController.h,ZASecondViewController.h,ZAThirdViewController.h均为UIViewcontroller
#import "ZAFirstViewController.h"
#import "ZASecondViewController.h"
#import "ZAThirdViewController.h"
@implementation ZAAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
//设置默认页面
ZAFirstViewController *firstVC=[[ZAFirstViewController alloc]init];
firstVC.title=@"1";
firstNV=[[UINavigationController alloc]initWithRootViewController:firstVC];
self.window.rootViewController=firstNV;
[self.window makeKeyAndVisible];
//初始化自定义uitabbar
[self initTabbar];
return YES;
}
//初始化tabbarview
-(void)initTabbar
{
tabbarView=[[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.window.bounds)-40, 320, 40)];
tabbarView.backgroundColor=[UIColor lightTextColor];
tabbarView.userInteractionEnabled=YES;
[self.window addSubview:tabbarView];
for (int i=0; i<3; i++) {
// 例子只是简单的写了按钮,实际运用中可以添加图片等自定义下导航风格
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(320/3*i, 0, 320/3, 40);
[button setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
button.tag=i+1000;
[button addTarget:self action:@selector(tabbarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[tabbarView addSubview:button];
if (i==0) {
button.selected=YES;
}
}
}
// tabbar上的按钮点击事件
-(void)tabbarButtonClick:(UIButton *)button
{
int tag;
if (button.selected) {
//button已经是选中状态,不执行任何操作
return;
}
button.selected=!button.selected;
//将除当前选中button的selected状态设为No
for (UIButton *tabbutton in tabbarView.subviews) {
if (tabbutton!=button) {
tabbutton.selected=NO;
}
}
tag=button.tag%10;
//根据当前button的tag值 切换self.window的rootViewController,
[self changeRootViewWithTag:tag];
}
//切换self.window的rootViewController,实现页面切换的效果
-(void)changeRootViewWithTag:(int)tag
{
switch (tag) {
case 0:
if (!firstNV) {
ZAFirstViewController *firstVC=[[ZAFirstViewController alloc]init];
firstVC.title=@"1";
firstNV=[[UINavigationController alloc]initWithRootViewController:firstVC];
}
self.window.rootViewController=firstNV;
break;
case 1:
if (!secondNV) {
ZASecondViewController *secondVC=[[ZASecondViewController alloc]init];
secondVC.title=@"2";
secondNV=[[UINavigationController alloc]initWithRootViewController:secondVC];
}
self.window.rootViewController=secondNV;
break;
case 2:
if (!thirdNV) {
ZAThirdViewController *thirdVC=[[ZAThirdViewController alloc]init];
thirdVC.title=@"3";
thirdNV=[[UINavigationController alloc]initWithRootViewController:thirdVC];
}
self.window.rootViewController=thirdNV;
break;
default:
break;
}
// 将tabbarview 显示到最上层
UIWindow *keyWindow=[[UIApplication sharedApplication] keyWindow];
[keyWindow bringSubviewToFront:tabbarView];
}