我是靠谱客的博主 冷艳中心,最近开发中收集的这篇文章主要介绍iOS 标签栏和导航栏,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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该对象之后,标签栏还在。


最后

以上就是冷艳中心为你收集整理的iOS 标签栏和导航栏的全部内容,希望文章能够帮你解决iOS 标签栏和导航栏所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部