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); }
4: 属性小总结

属性.png
![]()
a.png
作者:by小杰
链接:http://www.jianshu.com/p/ec34a4bfce11
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。