1.将控制器添加到导航栏控制器,并将导航栏添加到标签栏控制器中,设置标签栏控制器为根视图控制器
UITabBarController *tabBarVC = [[UITabBarController alloc]init];
//1.构建一个控制器数组:
HomeViewController *homeVC =[[HomeViewController alloc]init];
FindViewController *findVC=[[FindViewController alloc]init];
ShopcartViewController *shopcartVC=[[ShopcartViewController alloc]init];
MineViewController *mineVC =[[MineViewController alloc]init];
//每一个tabBarItem添加一个navigation
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:homeVC];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:findVC];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:shopcartVC];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:mineVC];
//导航栏背景
[navController1.navigationBar setBackgroundImage: [UIImage imageNamed:@"navBG.png"] forBarMetrics:UIBarMetricsDefault];
[navController2.navigationBar setBackgroundImage: [UIImage imageNamed:@"navBG.png"] forBarMetrics:UIBarMetricsDefault];
[navController3.navigationBar setBackgroundImage: [UIImage imageNamed:@"navBG.png"] forBarMetrics:UIBarMetricsDefault];
[navController4.navigationBar setBackgroundImage: [UIImage imageNamed:@"navBG.png"] forBarMetrics:UIBarMetricsDefault];
//将navigation添加到tabBar中
NSArray* controllers = [NSArray arrayWithObjects:navController1, navController2, navController3, navController4,nil];
tabBarVC.viewControllers = controllers;
self.window.rootViewController = tabBarVC;
2.设置标签栏标题和图片
-(instancetype)init
{
//指定各标题栏的标题和图片
self=[super init];
if (self) {
self.tabBarItem.title=@"首页";
self.tabBarItem.image=[UIImage imageNamed:@"bottom_tab_home_page_unsel.png"];
}
return self;
}
2.设置导航栏标题,并自定义左右按钮
//标题
UILabel *titleText = [[UILabel alloc] initWithFrame: CGRectMake((VWIDTH-80)/2, 0, 50, 50)];
titleText.backgroundColor = [UIColor clearColor];
titleText.textColor=[UIColor whiteColor];
[titleText setFont:[UIFont fontWithName:@"Helvetica-Bold"size:20]];
[titleText setText:@"微商"];
self.navigationItem.titleView = titleText;
//创建右边导航按钮
UIButton*rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,25,25)];
[rightButton setImage:[UIImage imageNamed:@"msg.png"]forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(message:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightItem;
//创建左边导航按钮
UIButton *leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,25,25)];
[leftButton setImage:[UIImage imageNamed:@"left_img.png"]forState:UIControlStateNormal];
[leftButton addTarget:self action:@selector(search:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItem = leftItem;
3.设置标签栏选中背景色
self.tabBarController.tabBar.selectedImageTintColor = [UIColor redColor];
4.自定义返回按钮
//返回按钮
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(0, 0, 25, 25);
[backBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
self.navigationItem.leftBarButtonItem = backItem;
5.隐藏系统标签栏
//隐藏标签栏
self.hidesBottomBarWhenPushed = YES;
在push到下个控制器之前隐藏,将self换为要push的对象,怎pop该对象之后,标签栏还在。