1、UITabBarController的使用:
tabBar是UITabBar的对象,包含多个tabBarItem,每个item对应一个UIViewController,当tabBarItem超过五个时,会出现一个更多按钮
2.重要属性
viewControllers:要显示的视图控制器,是一个数组
tabBar:标签栏
代理:self
3.主要代码
将tabBarController作为窗口的根视图控制器
#import "AppDelegate.h"
//2.遵循代理协议
@interface AppDelegate ()<UITabBarControllerDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
#pragma mark--------创建多个被导航控制器包裹的视图控制器
RedViewController *redVC = [RedViewController new];
UINavigationController *redNC = [[UINavigationController alloc] initWithRootViewController:redVC];
OrangeViewController *orangeVC = [OrangeViewController new];
UINavigationController *orangeNC = [[UINavigationController alloc] initWithRootViewController:orangeVC];
YellowViewController *yellowVC = [YellowViewController new];
UINavigationController *yellowNC = [[UINavigationController alloc] initWithRootViewController:yellowVC];
GreenViewController *greenVC = [GreenViewController new];
UINavigationController *greenNC = [[UINavigationController alloc] initWithRootViewController:greenVC];
CyanViewController *cyanVC = [CyanViewController new];
UINavigationController *cyanNC = [[UINavigationController alloc] initWithRootViewController:cyanVC];
BlueViewController *blueVC = [BlueViewController new];
UINavigationController *blueNC = [[UINavigationController alloc] initWithRootViewController:blueVC];
PurpleViewController *purpleVC = [PurpleViewController new];
UINavigationController *purpleNC = [[UINavigationController alloc] initWithRootViewController:purpleVC];
#pragma mark------------tabBarController-------
//创建tabBarController
UITabBarController *tabBC = [UITabBarController new];
//准备视图控制器(子视图控制器)
NSArray *array = @[redNC,orangeNC,yellowNC,greenNC,cyanNC,blueNC,purpleNC];
//设定子视图控制器
tabBC.viewControllers = array;
//设置标题颜色
tabBC.tabBar.tintColor = [UIColor magentaColor];
//设置tabBar的背景颜色
//tabBC.tabBar.barTintColor = [UIColor lightGrayColor];
//tabBar的默认高度为49
//tabBar的样式
//tabBC.tabBar.barStyle = UIBarStyleBlack;
//添加背景图片
//tabBC.tabBar.backgroundImage = [UIImage imageNamed:@"image1"];
//为窗口指定根视图控制器
self.window.rootViewController = tabBC;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
#pragma mark---------添加tabBarItem点击事件
//设置选中的下标
tabBC.selectedIndex = 0;
// NSLog(@"%ld",tabBC.selectedIndex);
#pragma mark---------带值value
//设置badgeValue(设置标记)
redNC.tabBarItem.badgeValue = @"New";
#pragma mark------------实现tabBarController的代理
//1.设置代理对象
tabBC.delegate = self;
#pragma mark------------一键换肤
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
return YES;
}
//3.实现代理方法
//将要选中当前控制器的触发方法
//一般在该方法中先开始解析数据,加载一个可以替换的视图
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSLog(@"%d,%s",__LINE__,__FUNCTION__);
return YES;
}
//已经选中当前控制器的触发方法
//通常在点击到该视图控制器中替换视图
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"%d,%s",__LINE__,__FUNCTION__);
}
4.创建tabBar的三种方法
#pragma mark------------三种创建tabBarItem
//根据给定的系统样式来创建
// self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:1];
//根据标题设置设置自定义的标题项
// self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Red" image:[UIImage imageNamed:@"07-map-marker.png"] selectedImage:[UIImage imageNamed:@"07-map-marker.png"]];
//自定义标题和图片
// self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Red" image:[UIImage imageNamed:@"99-umbrella"] tag:1];
// //渲染
// UIImage *image = [UIImage imageNamed:@"shishi.jpg"];
//
// //渲染为原色版本
// image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//
// //自定义标题和图片
// self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"红" image:image tag:1];
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"iconfont-shouye"] tag:1];
}