UITabBar背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                UITabBar背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                一、對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背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: HTML5如何重塑O2O用户体验
- 下一篇: 【微信网页版】给所有微信群发消息
