iOS UITabBarController的简单使用

一、UITabBarController 使用详解

    UITabBarController是IOS中很常用的一个viewController,例如系统的闹钟程序,ipod程序等。UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的container viewController中。

  首先我们看一下它的view层级图:

    

二、手动创建UITabBarController

  最常见的创建UITabBarController的地方就是在application delegate中的 applicationDidFinishLaunching:方法,因为UITabBarController通常是作为整个程序的rootViewController的,我们需要在程序的window显示之前就创建好它,具体步骤如下:

  1、初始化UITabBarController

 //初始化UITabBarController
    UITabBarController *tabbarCon=[[UITabBarController alloc] init];

  2、创建tabbarController中每一个tab对应的要显示的对象,就是子视图

   HHFirstViewController *first=[[HHFirstViewController alloc] initWithNibName:@"HHFirstViewController" bundle:nil];
    UINavigationController *firstNav=[[UINavigationController alloc] initWithRootViewController:first];

  3、将tab对应的对象添加到UITabBarController中  

   //将对象添加到tabbarcon中
    [tabbarCon setViewControllers:@[firstNav,secondNav,threeNav]];

  4、通过设置UITabBarController对象为window.rootViewController,然后显示window 

   self.window.rootViewController=tabbarCon;
    [self.window makeKeyAndVisible];

  根据以上4个步骤,UITabBarController的创建基本完全,但是不能满足我们的正常中的开发,还需要涉及到一些基本的属性。  

 

三、UITabBarController基本属性

  1、设置tabbar的标题title. 

@property(nonatomic,copy)             NSString    *title;        // default is nil

  2、设置常量图片

@property(nonatomic,retain)           UIImage     *image;        // default is nil

  3、设置高量图片

@property(nonatomic,retain) UIImage *selectedImage NS_AVAILABLE_IOS(7_0);

  4、设置选中tabbar的字体颜色

/* selectedImageTintColor will be applied to the gradient image used when creating the
 selected image. Default is nil and will result in the system bright blue for selected
 tab item images. If you wish to also customize the unselected image appearance, you must
 use the image and selectedImage properties on UITabBarItem along with UIImageRenderingModeAlways

 Deprecated in iOS 8.0. On iOS 7.0 and later the selected image takes its color from the
 inherited tintColor of the UITabBar, which may be set separately if necessary.
 */
@property(nonatomic,retain) UIColor *selectedImageTintColor NS_DEPRECATED_IOS(5_0,8_0,"Use tintColor") UI_APPEARANCE_SELECTOR;

 完成这4个属性,我们的UITabBarController就能完全基本的功能。如下图:

               

下面就是创建UITabBarController的全部代码:

 self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];

    HHFirstViewController *first=[[HHFirstViewController alloc] initWithNibName:@"HHFirstViewController" bundle:nil];
    UINavigationController *firstNav=[[UINavigationController alloc] initWithRootViewController:first];
    HHSecondViewController *second=[[HHSecondViewController alloc] initWithNibName:@"HHSecondViewController" bundle:nil];
    UINavigationController *secondNav=[[UINavigationController alloc]initWithRootViewController:second];
    HHThreeViewController *three=[[HHThreeViewController alloc] initWithNibName:@"HHThreeViewController" bundle:nil];
    UINavigationController *threeNav=[[UINavigationController alloc] initWithRootViewController:three];

    //初始化UITabBarController
    UITabBarController *tabbarCon=[[UITabBarController alloc] init];
    //将对象添加到tabbarcon中
    [tabbarCon setViewControllers:@[firstNav,secondNav,threeNav]];

     tabbarCon.tabBar.selectedImageTintColor=[UIColor orangeColor];//设置选中的字体颜色

    UITabBar *tabBar=tabbarCon.tabBar;
    UITabBarItem *itemFirst=[tabBar.items objectAtIndex:0];
    itemFirst.title=@"优贷通";
    UITabBarItem *itemSecond=[tabBar.items objectAtIndex:1];
    itemSecond.title=@"个人中心";
    UITabBarItem *itemThree=[tabBar.items objectAtIndex:2];
    itemThree.title=@"更多";

    //设置常量图片
    itemFirst.image=[[UIImage imageNamed:@"商户_normol.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //设置选中的图片
    itemFirst.selectedImage=[[UIImage imageNamed:@"商户_press.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    itemSecond.image=[[UIImage imageNamed:@"我的_normol.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    itemSecond.selectedImage=[[UIImage imageNamed:@"我的_press"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    itemThree.image=[[UIImage imageNamed:@"更多_normol.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    itemThree.selectedImage=[[UIImage imageNamed:@"更多_press.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    self.window.rootViewController=tabbarCon;
    [self.window makeKeyAndVisible];

说明:UITabBarButton在UITabBar中得位置是均分的,UITabBar的高度为49。图标的大小为30,或者等比例。

时间: 2024-08-25 22:38:45

iOS UITabBarController的简单使用的相关文章

ios状态栏调整 简单动画的知识点

首先状态栏式view的属性,所以在viewController中直接重写: /** 修改状态栏 */ - (UIStatusBarStyle)preferredStatusBarStyle { // 修改状态栏的颜色(白色) return UIStatusBarStyleLightContent; } // 这种返回值没有*的说明不是对象,那么不是枚举就是结构体,大多数情况是枚举,所以取值也比较简单,一般是返回值后边加上状态: 在UIKit学习中常用的块动画: // 块动画 animateWit

iOS设计模式 - (3)简单工厂模式

iOS设计模式 - (3)简单工厂模式           by Colin丶 转载请注明出处:              http://blog.csdn.net/hitwhylz/article/details/40381721 一.简述 简单工厂模式(FACTORY),通过面向对象的封装,继承和多态来降低程序的耦合度.将一个具体类的实例化交给一个静态工厂方法来执行. 该模式中的角色包括: 工厂类(Simple Factory): 只包含了创建具体类的静态方法. 抽象产品(Product):

iOS progressview的简单使用

head 代码.. Java代码   #import <UIKit/UIKit.h> @interface ViewController : UIViewController { UIProgressView *progressview; UIProgressView *progressviewbar; NSTimer *timer; } @property(retain,nonatomic) IBOutlet UIProgressView *progressview; @property(r

ios下最简单的正则,RegexKitLite

ios下最简单的正则,RegexKitLite 1.去RegexKitLite下载类库,解压出来会有一个例子包及2个文件,其实用到的就这2个文件,添加到工程中.备用地址:http://www.cocoachina.com/bbs/job.php?action-download-pid-135286-tid-18111-aid-11143.html- Lv 2.工程中添加libicucore.dylib frameworks. 3.现在所有的nsstring对象就可以调用RegexKitLite中

ios基础篇(八)——UITabBarController的简单介绍

一.简介 UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例子就是QQ.微信.微博等应?. 二.UITabBarController的使用 1.首先初始化UITabBarController 2.设置UIWindow的rootViewController为UITabBarController 3.创建相应的子控制器(viewcontroller) 4.把子控制器

iOS边练边学--UITabBarController的简单使用

一.UITabBarController的使用步骤 初始化UITabBarController 设置UIWindow的rootViewController为UITabBarController 根据具体情况,通过addChildViewController方法添加对应个数的子控制器 代码实现: UITabBarController添加控制器的方式有两种 添加单个子控制器 - (void)addChildViewController:(UIViewController *)childControl

iOS 制作一个简单的画板

制作简单画板 作为iOS初学者,在学习完UI的几个简单控件(UILable,UITextField,UIButton)之后,就可以制作一个简单的画图板demo,以下是具体制作流程(在MRC下),如有不足之处,还请各位大神们指教 0.0. 1.搭建界面,主要由UIButton,UITextField组成,底部的按钮是UITextField的一个自定义键盘(inputView) . - (void)viewDidLoad { [super viewDidLoad]; //创建菜单按钮 UIButto

iOS block-base 动画简单用法+关键帧动画设置线性变化速度的问题

本文转载至 http://www.tuicool.com/articles/aANBF3m 时间 2014-12-07 20:13:37  segmentfault-博客原文  http://segmentfault.com/blog/alan/1190000002411296 iOS的各种动画相漂亮,相信这是吸引很多人买iPhone的原因之一.不仅如此,这还是吸引我做iOS开发的一大原因,因为在iOS上给界面实现一些像样的动画实在是太轻松了! 这里就介绍一下iOS的block-based an

Cisco路由器在rommon状态下恢复IOS的最简单方法

这是很多年前的事了.一天突然监控到一台2610的路由器中断,跑到现场用Console登录后,出现ROMMON 1 >提示,而无法进入到正式的IOS系统. 第一反应就是Flash中的IOS被破坏或丢失了,经过简单的几个步骤,路由器很快就恢复了正常.网络上有介绍好几种方法,但我觉得TFTP的方法是效率最高的,我也重点推荐采用该方法. 在一台机器上安装TFTP服务器软件,用大家最熟悉的3CDaemon就好了,将IOS文件放置在TFTP服务器的默认根目录下,打开TFTP服务器,用控制线将这台机器与ROU