标簽視圖控制器 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