先看效果圖git
通常爲了實現底部導航和跳轉子控制器.通常都是UITabBarController和UINavigationController嵌套使用app
通常將ViewController添加到UINavigationController管理, 而後將UINavigationController添加到UITabBarController, 再將UITabBarController添加到rootViewController .框架
ViewController -> UINavigationController -> UITabBarController -> rootViewControlleride
其中UITabBarController添加多個UINavigationController或ViewController以實現評級視圖的切換.字體
直接看代碼吧,代碼有註釋ui
AppDelegatethis
// // AppDelegate.m // TabBarTest // // Created by 羅若文 on 2016/11/1. // Copyright © 2016年 羅若文. All rights reserved. // #import "AppDelegate.h" #import "ViewController.h" #import "NIImage.h" #import "NITabBarController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //1.建立Window self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; //a.初始化一個tabBar控制器 UITabBarController *tb=[[NITabBarController alloc]init]; //b.建立子控制器 ViewController *c1=[[ViewController alloc]init]; //將子控制器添加到UINavigationController中,方便控制器深度的跳轉 UINavigationController * navigationController=[[UINavigationController alloc]initWithRootViewController:c1]; //設置底部的標題,若是沒有設置默認會取控制器的title. 由於c1是在UINavigationController中因此用這樣的方式c1.navigationController取到tabBarItem進行設置 c1.navigationController.tabBarItem.title=@"消息"; //底部的圖片 c1.navigationController.tabBarItem.image=[NIImage imageNamed:@"app0.png" targetSize:CGSizeMake(50, 50)]; //設置提示badge c1.navigationController.tabBarItem.badgeValue=@"3"; UIViewController *c2=[[UIViewController alloc]init]; c2.view.backgroundColor=[UIColor brownColor]; //這個控制器沒有添加到UINavigationController中,就能夠直接c2.tabBarItem.title設置底部標題 c2.tabBarItem.title=@"聯繫人"; c2.tabBarItem.image=[NIImage imageNamed:@"聯繫人0.png" targetSize:CGSizeMake(30, 30)]; UIViewController *c3=[[UIViewController alloc]init]; c3.tabBarItem.title=@"動態"; c3.tabBarItem.image=[NIImage imageNamed:@"uncheck.png" targetSize:CGSizeMake(30, 30)]; //設置底部選中時候的圖片 c3.tabBarItem.selectedImage=[NIImage imageNamed:@"check.png" targetSize:CGSizeMake(30, 30)]; //修改圖片的位置(上,左,下,右) 上和下的要對應偏移,否則會有意向不到的效果,本身試試, 左右也同樣 c3.tabBarItem.imageInsets=UIEdgeInsetsMake(10,-30,-10, 30); //修改文字的位置(水平,垂直) [c3.tabBarItem setTitlePositionAdjustment:UIOffsetMake(10, -30)]; UIViewController *c4=[[UIViewController alloc]init]; c4.tabBarItem.title=@"設置"; c4.tabBarItem.image=[NIImage imageNamed:@"set0.png" targetSize:CGSizeMake(30, 30)]; //設置底部選中時候的圖片 c4.tabBarItem.selectedImage=[NIImage imageNamed:@"set1.png" targetSize:CGSizeMake(30, 30)]; //設置選中圖片保持原來圖片顏色,未選中也同樣設置UIImageRenderingModeAlwaysOriginal c4.tabBarItem.selectedImage= [c4.tabBarItem.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // 獲取當前類的tabBarItem UITabBarItem *BarItem = c4.tabBarItem; // 設置全部BarItem的選中時顏色 // 設置選中文字顏色 // 建立字典去描述文本 NSMutableDictionary *attr = [NSMutableDictionary dictionary]; // 文本顏色 -> 描述富文本屬性的key -> NSAttributedString.h attr[NSForegroundColorAttributeName] = [UIColor blueColor]; [BarItem setTitleTextAttributes:attr forState:UIControlStateSelected]; // 經過normal狀態設置字體大小 // 字體大小 跟 normal NSMutableDictionary *attrnor = [NSMutableDictionary dictionary]; // 設置字體 attrnor[NSFontAttributeName] = [UIFont systemFontOfSize:16]; [BarItem setTitleTextAttributes:attrnor forState:UIControlStateNormal]; //c.添加子控制器到ITabBarController中 //c.1第一種方式 //[tb addChildViewController:c1]; //[tb addChildViewController:c2]; //c.2第二種方式 tb.viewControllers=@[navigationController,c2,c3,c4]; //設置標籤欄文字和圖片的顏色 tb.tabBar.tintColor = [UIColor orangeColor]; //設置標籤欄的顏色 tb.tabBar.barTintColor = [UIColor blackColor]; //設置標籤欄風格(默認高度49) tb.tabBar.barStyle = UIBarStyleBlack; //設置初始狀態選中的下標 tb.selectedIndex = 2; //設置選中的背景 float w=[UIScreen mainScreen].bounds.size.width/4.0; tb.tabBar.selectionIndicatorImage=[NIImage imageByScalingToSize:[NIImage imageWithColor:[UIColor greenColor]] targetSize:CGSizeMake(w, 80)]; //設置控制器爲Window的根控制器 self.window.rootViewController=tb; //2.設置Window爲主窗口並顯示出來 [self.window makeKeyAndVisible]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
若是須要設置tabBar的高度,那麼就要重寫一下UITabBarController中的viewWillLayoutSubviews方法.net
// // NITabBarController.m // TabBarTest // // Created by 羅若文 on 2016/11/1. // Copyright © 2016年 羅若文. All rights reserved. // #import "NITabBarController.h" @interface NITabBarController () @end @implementation NITabBarController - (void)viewDidLoad { [super viewDidLoad]; //這邊重構TabBarController視圖 } /** 這邊從新設置TabBar的高度 */ - (void)viewWillLayoutSubviews{ CGRect tabFrame = self.tabBar.frame; //self.TabBar is IBOutlet of your TabBar tabFrame.size.height = 80; tabFrame.origin.y = self.view.frame.size.height - 80; self.tabBar.frame = tabFrame; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
在你用[self.navigationController pushViewController:vc animated:YES];跳轉到第二個視圖的時候會發現底部的tabBar還在.那麼若是不想要顯示,就在跳轉前設置一下hidesBottomBarWhenPushed屬性就好了rest
ViewController2 * vc=[[ViewController2 alloc]init]; vc.hidesBottomBarWhenPushed=YES; [self.navigationController pushViewController:vc animated:YES];
這邊有引用的NIImage.h在新創意的開源框架經常使用類中能夠找到.其餘的代碼主要都在這裏了.都能知足須要了code