1. UITabBarController———标签栏视图控制器
2. UITabBarController 分为3层结构: TabBarControlView TabBar custumContent
3. tabBar的重要属性以及用法:
// 1. 自定义视图控制器
OneViewController *oneVC = [[OneViewController alloc] init];
TwoViewController *twoVC = [[TwoViewController alloc] init];
ThreeViewController *threeVC = [[ThreeViewController alloc] init];
FourViewController *fourVC = [[FourViewController alloc] init];
FiveViewController *fiveVC = [[FiveViewController alloc] init];
SixViewController *sixVC = [[SixViewController alloc] init];
// 2. 创建一个UITabBarController
UITabBarController *tabBarVC = [[UITabBarController alloc] init];
// 3. 添加导航控制器
UINavigationController *oneNC = [[UINavigationController alloc] initWithRootViewController:oneVC];
UINavigationController *twoNC = [[UINavigationController alloc] initWithRootViewController:twoVC];
UINavigationController *threeNC = [[UINavigationController alloc] initWithRootViewController:threeVC];
UINavigationController *fourNC = [[UINavigationController alloc] initWithRootViewController:fourVC];
UINavigationController *fiveNC = [[UINavigationController alloc] initWithRootViewController:fiveVC];
UINavigationController *sixNC = [[UINavigationController alloc] initWithRootViewController:sixVC];
// 4. 把控制器给UITabBarController去控制显示
tabBarVC.viewControllers = @[oneNC, twoNC, threeNC, fourNC, fiveNC, sixNC];
// 把UITabBarController设置成根视图控制器
self.window.rootViewController = tabBarVC;
// 更改bar的颜色
tabBarVC.tabBar.barTintColor = [UIColor colorWithWhite:0.000 alpha:0.460];
// 更改选中bar的颜色
tabBarVC.tabBar.tintColor = [UIColor colorWithRed:1.000 green:0.553 blue:0.654 alpha:1.000];
// 添加tabBar的背景图片
[tabBarVC.tabBar setBackgroundImage:[UIImage imageNamed:@"tabBar"]];
// tabBarItems的高度为49
// 添加tabBarItems图片, 必须是PNG格式的, 图片的大小必须是30*30以内的. 图片必须是镂空图
oneNC.tabBarItem.image = [UIImage imageNamed:@"01-refresh"];
twoNC.tabBarItem.image = [UIImage imageNamed:@"02-redo"];
threeNC.tabBarItem.image = [UIImage imageNamed:@"03-loopback"];
fourNC.tabBarItem.image = [UIImage imageNamed:@"04-squiggle"];
fiveNC.tabBarItem.image = [UIImage imageNamed:@"05-shuffle"];
sixNC.tabBarItem.image = [UIImage imageNamed:@"06-magnifying-glass"];
// 添加标题
oneNC.tabBarItem.title = @"首页";
twoNC.tabBarItem.title = @"two";
threeNC.tabBarItem.title = @"three";
fourNC.tabBarItem.title = @"four";
fiveNC.tabBarItem.title = @"five";
sixNC.tabBarItem.title = @"six";
// 添加点点击后更换的图片
oneNC.tabBarItem.selectedImage = [UIImage imageNamed:@"02-redo"];
twoNC.tabBarItem.selectedImage = [UIImage imageNamed:@"06-magnifying-glass"];
threeNC.tabBarItem.selectedImage = [UIImage imageNamed:@"04-squiggle"];
fourNC.tabBarItem.selectedImage = [UIImage imageNamed:@"01-refresh"];
fiveNC.tabBarItem.selectedImage = [UIImage imageNamed:@"03-loopback"];
sixNC.tabBarItem.selectedImage = [UIImage imageNamed:@"05-shuffle"];
// 左上角的小红标, 传入的是字符串
oneNC.tabBarItem.badgeValue = @"99+";
twoNC.tabBarItem.badgeValue = @"10";
threeNC.tabBarItem.badgeValue = @"new";
// 让tabBar默认选中第二个页面, 索引从0开始
tabBarVC.selectedIndex = 1;
// 设置代理
tabBarVC.delegate = self;
主要使用的代理方法:
// 选中后触发的方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(@"这是第%ld个界面", tabBarController.selectedIndex);
// 代理方法应用场景: 点击回来, 刷新数据, 更改界面
}