zoukankan      html  css  js  c++  java
  • 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];
    View Code

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

  • 相关阅读:
    传球接力
    业务办理
    P2077 红绿灯
    【UR #4】元旦激光炮
    P1939 【模板】矩阵加速(数列)
    #82. 【UR #7】水题生成器
    Visible Trees HDU
    创始人的领导力和合伙人选择
    面向对象笔试题练习一
    MicroPython+北斗+GPS+GPRS:TPYBoardv702短信功能使用说明
  • 原文地址:https://www.cnblogs.com/boyuanmeng/p/4786545.html
Copyright © 2011-2022 走看看