iOS UITabbarController

1:UITabBarController & UINavigationController的区别

应用场景不同:
导航视图控制器 -->管理有层次关系的视图控制器(依赖关系)
标签视图控制器 -->管理没有层次关系的视图控制器(同时存在,互不干扰)

管理方式不同:
导航视图控制器 -->以栈的形式管理多个试图控制器,push入栈,pop出栈,当返回上一界面时,空间回收
标签视图控制器 -->以不可变数组管理,而且创建时必须要全部指定所管理的多个视图控制器,而且多个视图控制器同时存在,空间不回收

2: 视图层次添加过程:

必须遵循该添加层次描述, 否则可能出现覆盖和不显示等问题!!!
UIWindow—>UITabBarcontroller—>UINavigationController— >UIViewController

3: 属性实例应用

本实例新建三个页面 ,实现同一层级上的三个页面的跳转,在第一个页面是添加一个触摸方法, 实现push到下一个页面

AppDelegate.m 文件

1: 创建三个视图控制器,并初始化视图控制器的tabBarItem

//初始化UIWindow

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

self.window.backgroundColor =[UIColor whiteColor];

[self.window makeKeyAndVisible];

//第一个标签视图

FirstViewController *firstVC = [FirstViewController new];

UINavigationController *firstNC = [[UINavigationController alloc] initWithRootViewController:firstVC];//第一种设置:TabBar外观方法(系统样式);

//第一种初始化方式(使用系统样式)

//参数1:系统tabBar外观样式

//参数2:tabBar的tag值

firstNC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:101];

//第二个标签视图

SeconViewController *second = [SeconViewController new];

UINavigationController *secondNC = [[UINavigationController alloc] initWithRootViewController:second];

//第二种初始化方式(TabBar自定义样式);

//默认状态下显示的图片

UIImage *secondNCimage = [UIImage imageNamed:@"carGary"];

//选中状态下显示的图片

UIImage *secondSelectedImage = [UIImage imageNamed:@"carRed"];

//图片不被渲染,保持原图

secondNCimage = [secondNCimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

secondSelectedImage = [secondSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

//第二种初始化方法:设置tabBar外观

//参数一:tabBar的标题

//参数二:未选中时的图片

//参数三:选中时的图片

secondNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"second" image:secondNCimage selectedImage:secondSelectedImage];

//第三个标签视图

//初始化视图控制器

ThirdViewController *thirdVC = [ThirdViewController new];

//将初始化的视图控制器作为NavigationController的根视图控制器

UINavigationController *thirdNC = [[UINavigationController alloc] initWithRootViewController:thirdVC];

//设置自定义图标

UIImage *thirdNCimage = [UIImage imageNamed:@"findGray"];

thirdNCimage = [thirdNCimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UIImage *thirdSelectedimage = [UIImage imageNamed:@"findRed"];

thirdSelectedimage = [thirdSelectedimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

//初始化TabBarItem

thirdNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"third" image:thirdNCimage selectedImage:thirdSelectedimage];

2: 创建UITabBarController

//1:创建一个UITabBarController(高:49)

UITabBarController *mainTabBar = [UITabBarController new];

//2:设置TabBarController的子视图控制器

mainTabBar.viewControllers = @[firstNC,secondNC,thirdNC];

//3:将根视图控制器设置为:TabBarController

[self.window setRootViewController:mainTabBar];

//设置tabBar选中时title的颜色(如果:TabBarItem是系统默认的样式,则设置该属性后,图标和文字同时改变颜色)

[mainTabBar.tabBar setTintColor:[UIColor redColor]];

//设置tabBar背景颜色

[mainTabBar.tabBar setBarTintColor:[UIColor colorWithRed:0.000 green:0.039 blue:1.000 alpha:1.000]];

//改变tabBar的位置

//[secondNC.tabBarItem setTitlePositionAdjustment:UIOffsetMake(30, 30)];

//设置进入程序后默认选中第几个

mainTabBar.selectedIndex = 0;

//设置提示信息

firstNC.tabBarItem.badgeValue = @"点我";

secondNC.tabBarItem.badgeValue = @"99+";

thirdNC.tabBarItem.badgeValue = @"放大";

//设置代理人

mainTabBar.delegate = self;

3: 实现代理方法

//当点击某个标签时,tabBar触发该方法

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{NSLog(@"%ld",tabBarController.selectedIndex); }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值