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);
}

截个图:

posted @ 2016-01-14 16:32  brave-sailor  阅读(535)  评论(0编辑  收藏  举报