UITabBarController整理

本文详细介绍如何使用UITabBarController创建带有导航栏的根视图控制器,并展示了如何为各个tab配置不同的内容,包括设置图标、文字颜色及角标提示等。


专题名:UITabBarController

UI:UITabBarItem图片尺寸:30*30px。

一、创建TabBae:

1、根页面创建:  

UITabBarController如果要作为整个程序的rootViewController,则在application delegate中的 applicationDidFinishLaunching:方法中添加如下代码:(以下代码带导航栏)

    HSHomeViewController *hsHome = [[HSHomeViewController alloc]init];
    UINavigationController *homeNav = [[UINavigationController alloc]initWithRootViewController:hsHome];
    
    HSOpenInfoViewController *hsOpen = [[HSOpenInfoViewController alloc]init];
    UINavigationController *openNav = [[UINavigationController alloc]initWithRootViewController:hsOpen];
    
    HSMyInfoViewController *hsMy = [[HSMyInfoViewController alloc]init];
    UINavigationController *myNav = [[UINavigationController alloc]initWithRootViewController:hsMy];
    
    HSMoreViewController *hsMore = [[HSMoreViewController alloc]init];
    UINavigationController *moreNav = [[UINavigationController alloc]initWithRootViewController:hsMore];
    
    UITabBarController * tabBar = [[UITabBarController alloc]init];
    tabBar.viewControllers =[NSArray arrayWithObjects:homeNav,openNav,myNav,moreNav, nil];
  
    self.window.rootViewController = tabBar;

二、改变每个tab的内容:

1、IOS7.0以上版本:创建方法:

UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:@"彩票大厅" image:[UIImage imageNamed:@"tab_home_false.png"] selectedImage:[UIImage imageNamed:@"tab_home_true.png"]];
<pre name="code" class="objc">viewController2.tabBarItem = item;

     IOS7.0以下版本:创建方法: 
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:2];
[item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"]  withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];
viewController2.tabBarItem = item;

2、属性badgeValue,该属性可以在其右上角显示一个小的角标,通常用于提示用户有新的消息。

3、改变tabBarItem的文字颜色:  

   [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
   [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], UITextAttributeTextColor, nil] forState:UIControlStateHighlighted];

4、改变tabBar的图标颜色:

    UITabBar *tabBar = [tabBarCtr tabBar];
    [tabBar setTintColor:[UIColor redColor]];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值