标签视图控制器 UITabBarController
ViewController //控制视图 控制n个viewController
//控制控制器
NavigationController //除了控制n个viewController还可以控制n个TabBarController
TabBarController //除了控制n个viewController还可以控制n个navigationController
NC控制多个TC TC再控制VC
TC控制多个NC NC再控制VC
注意:
如果tabBar上的图标有一层蒙版, 这种情况有可能是图标的图片有问题, 先检查图片大小是否超大, 或者图片是否重复等.
AppDelegate.m
#import "AppDelegate.h"
#import "TabBarViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// 创建好一个标签控制器
TabBarViewController * tabC = [[TabBarViewController alloc]init];
// 一般使用tabBarController作为根视图控制器
self.window.rootViewController = tabC;
//换肤 appearance是个单例
[[UITabBar appearance]setBarTintColor:[UIColor blueColor]];
[[UITabBar appearance]setBackgroundImage:[UIImage imageNamed:@"202.png"]];
//navigationBar换肤
[[UINavigationBar appearance]setBarTintColor:[UIColor yellowColor]];
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"202.png"] forBarMetrics:(UIBarMetricsDefault )];
return YES;
}
@end
TabBarViewController.m
#import "TabBarViewController.h"
#import "YellowViewController.h"
#import "BlueViewController.h"
#import "GreenViewController.h"
#import "RedViewController.h"
#import "GrayViewController.h"
#import "OrangaViewController.h"
@interface TabBarViewController ()<UITabBarControllerDelegate>
@end
@implementation TabBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//创建视图控制器 (tabBarItem设置在这里才会一起显示出来,当前默认显示出来的是第一个视图控制器,alloc init 此时并不会加载后面的视图控制器)
YellowViewController * yellowVC = [[YellowViewController alloc]init];
yellowVC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:110];
//角标
yellowVC.tabBarItem.badgeValue = @"99+";
//每个视图控制器单独设置自己的tabBarItem
BlueViewController * blueVC = [[BlueViewController alloc]init];
blueVC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:111];
blueVC.tabBarItem.badgeValue = @"哈罗";
GreenViewController * greenVC = [[GreenViewController alloc]init];
greenVC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:112];
greenVC.tabBarItem.badgeValue = @"hello";
RedViewController * redVC = [[RedViewController alloc]init];
redVC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:113];
redVC.tabBarItem.badgeValue = @"5";
GrayViewController * grayVC = [[GrayViewController alloc]init];
grayVC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:115];
OrangaViewController * orangaVC = [[OrangaViewController alloc]init];
orangaVC.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:116];
//所有显示的controller都显示在这个数组里
//这里最多可以显示5个,5个以上的会在第五个显示一个more图标,点即more图标可以看到没有显示的其他几个controller
//平常设计 建议最多不要超过4个
self.viewControllers = @[yellowVC,blueVC,greenVC,redVC];
//选中要显示的视图控制器,以数组下标为依据
self.selectedIndex = 0;
//自己设置自己的代理
self.delegate = self;
//换颜色
self.tabBar.barTintColor = [UIColor greenColor];
//换图片
// self.tabBar.backgroundImage = [UIImage imageNamed:@"202.png"];
// NSLog(@"Image = %@",[UIImage imageNamed:@"202.png"]);
//是每个UIViewController的
// self.navigationItem
}
#pragma mark ----tabBarController Delegate
//选中响应的controller
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLog(@"%@",viewController);
//取消小圆点
viewController.tabBarItem.badgeValue = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end