【iOS開發-31】UITabBar背景、icon表徵圖顏色、被選中背景設定以及隱藏UITabBar的兩種方式,-31uitabbar
一、對UITabBar背景和icon表徵圖的一些設定
(1)因為直接給UITabBar設定的背景顏色顯示的不純,半透明的感覺,所以,有時候我們可以直接利用純色的圖片作為背景達到想要的效果;
(2)給icon圖片改變顏色也是重要的實用方法之一,預設的時藍色。
在AppDelegate.m檔案中:(1個導航控制器和5個視圖控制器)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //建立5個視圖控制器和1個導航控制器 ViewController1 *vc1=[[ViewController1 alloc]init]; UINavigationController *nav1=[[UINavigationController alloc]initWithRootViewController:vc1]; ViewController2 *vc2=[[ViewController2 alloc]init]; ViewController3 *vc3=[[ViewController3 alloc]init]; ViewController4 *vc4=[[ViewController4 alloc]init]; ViewController5 *vc5=[[ViewController5 alloc]init]; ViewController *vc6=[[ViewController alloc]init]; //6個標題 nav1.title=@"介面1"; vc2.title=@"介面2"; vc3.title=@"介面3"; vc4.title=@"介面4"; vc5.title=@"介面5"; vc6.title=@"介面6"; //6個系統icon表徵圖 [nav1.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:1]; [vc2.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemSearch tag:2]; [vc3.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemContacts tag:3]; [vc4.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:4]; [vc5.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemMostRecent tag:5]; [vc6.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:6]; //建立一個視圖控制器數組,並把它賦值給標籤欄控制器的viewControllers值 NSArray *arr1=[[NSArray alloc]initWithObjects:nav1,vc2,vc3,vc4,vc5,vc6, nil]; UITabBarController *tbCon1=[[UITabBarController alloc]init]; tbCon1.viewControllers=arr1; //標籤欄控制器有個tabBar屬性,這個屬性有兩個items和selectedItem屬性是不能用的,因為這兩個屬性是歸標籤欄控制器直接管理,其他人不能對其賦值 //運行以下兩行代碼,程式會崩潰 //tbCon1.tabBar.items=[[NSArray alloc]initWithObjects:vc1.tabBarItem, nil]; //tbCon1.tabBar.selectedItem=vc1.tabBarItem; //通過backgroundColor可以設定標籤欄顏色,但是是一層淡淡的紅色 tbCon1.tabBar.backgroundColor=[UIColor redColor]; //可以通過設定背景圖片的方式給標籤欄設定背景顏色,比如紅色的背景圖片,要求圖片大小要正好 //用以下方式獲得標籤欄寬高後,建立一個背景圖片,再引入進來 NSLog(@"%i,%i",(int)tbCon1.tabBar.frame.size.height,(int)tbCon1.tabBar.frame.size.width); tbCon1.tabBar.backgroundImage=[UIImage imageNamed:@"tabBarbg.png"]; //通過tintColor可以給icon表徵圖設定顏色 tbCon1.tabBar.tintColor=[UIColor redColor]; //設定被選中標籤的背景圖片,寬度是375/5=77 tbCon1.tabBar.selectionIndicatorImage=[UIImage imageNamed:@"selectionDic.png"]; //把這個標籤欄控制器當做window的根視圖控制器來顯示 self.window.rootViewController=tbCon1; // Override point for customization after application launch. return YES;}
二、隱藏UITabBar的第一種方式
這一種方式需要用導航控制器視圖來做實驗,因為我們需要用hidesBottomBarWhenPushed屬性,這個屬性的意思是,當這個視圖被壓到棧中時(導航控制器的棧),隱藏底部的bar,包括UITabBar。
所以我們以上面的nav1做實驗,nav1的根視圖控制器是vc1,我們在vc1中增加一個按鈕,一點擊就到ViewController7.m中(執行個體是vc7),並隱藏UITabBar。
在vc1中:
#import "ViewController1.h"#import "ViewController7.h"@interface ViewController1 ()@end@implementation ViewController1- (void)viewDidLoad { // UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btn1.frame=CGRectMake(38, 80, 300, 30); btn1.backgroundColor=[UIColor whiteColor]; [btn1 setTitle:@"PUSH" forState:UIControlStateNormal]; [btn1 addTarget:self action:@selector(jumpTo) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn1]; [super viewDidLoad]; // Do any additional setup after loading the view.}-(void)jumpTo{ ViewController7 *vc7=[[ViewController7 alloc]init]; [self.navigationController pushViewController:vc7 animated:NO];}@end
在ViewController7.m中:
#import "ViewController7.h"@interface ViewController7 ()@end@implementation ViewController7//增加一個initWithNibName方法,標配是return self。此外還需要在初始化時就設定它的hidesBottomBarWhenPushed屬性為YES才會生效//即,在視圖控制器的執行個體被載入到棧之前,就需要設定這個屬性,否則無效-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self.hidesBottomBarWhenPushed=YES; return self;}@end
三、隱藏UITabBar的第二種方式
就是把UITabBar的位置移動,即調整frame.origin.y的值。消失就是把它移出螢幕,出現就是把它再放回原地。(但,還原時又卡頓現象,體驗很差,不建議)
所以,在ViewController7.m中:
//增加一個試圖即將出現時的方法,並在此設定把tabBar下移,移除整個螢幕,相當於消失了-(void)viewDidAppear:(BOOL)animated{ NSArray *arr1=self.tabBarController.view.subviews; UIView *view1=[arr1 objectAtIndex:0]; UITabBar *tabBarView1=[arr1 objectAtIndex:1]; //第一個視圖就是全屏,不需要把高度撐滿,所以可以不做任何設定 //view1.frame=CGRectMake(0, 0, 375, 667); tabBarView1.frame=CGRectMake(0, 667, 375, 49);}
在ViewController1.m中:
//增加一個viewDidAppear,把下移的tabBar再上移,相當於還原到原地-(void)viewDidAppear:(BOOL)animated{ NSArray *arr2=self.tabBarController.view.subviews; UITabBar *tabBarView2=[arr2 objectAtIndex:1]; tabBarView2.frame=CGRectMake(0, 618, 375, 49);}
其實還有一種隱藏方式,但是極具殺傷力,一隱全隱了,就是從根源上設定這個標籤控制器不顯示UITabBar。就是下面的第二行代碼:
UITabBarController *tbCon1=[[UITabBarController alloc]init]; tbCon1.tabBar.hidden=YES;
截個圖:
IOS開發的UITabBarController的一點問題有分
在組裝uitabbarcontroller的代碼中來指定各個uitabbaritem.而不是在各個被包含的viewcontroller中
UITabbarController *tabController=[[UITabbarController alloc] init];
self.vc1=[[UIViewController alloc] init];
slef.vc1.tabbarItem=......;
self.vc2=[[UIViewController alloc] init];
self.vc2.tabbarItem = ....;
......
NSArray *vcs=@[self.vc1,self.vc2,....];
[tabController setViewControllers:vcs];
可以發下iOS開發視頻教程 UI:63 UITabBarController的層次關係mov的種子或下載連結?
iOS開發視頻教程 UI:6.3 UITabBarController的層次關係.mov種子:
thunder://QUFodHRwOi8vYWlrYW5keS5vcmcvaU9T5byA5Y+R6KeG6aKR5pWZ56iLIFVJ77yaNi4zIFVJVGFiQmFyQ29udHJvbGxlcueahOWxguasoeWFs+ezuy5tb3Y/ZmlkPWpWKm9CWjIzaVRoREVETnZNM25ES0E2ZWMyVUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUEmbWlkPTY2NiZ0aHJlc2hvbGQ9MTUwJnRpZD1FNTFERDdCQzAxQTkyODJCQTc3RUMxQzNCRUIzQ0JGNyZzcmNpZD0xMjAmdmVybm89MVpa
採納!做一個有道德的觀眾