UITabBarController

1.创建一个UITableController

2.为每个Tab创建一个视图控制器

3.创建一个array  把视图控制器都添加到array中 在把array放到UITabBarCtrl的Controllers中

4.设置根视图为tabBar

*只有在支持旋转的设置  才会旋转  发生旋转时只有当前的ViewController才会接受到旋转的消息

*对于UITabBar自带的tabBar  不能直接去修改

//修改背景图片

UIImage *tabBackground = [UIImage imageNamed:@"main_title2.png"];

   if ([tabBarController.tabBar respondsToSelector:@selector(setBackgroundImage:)]){

        [tabBarController.tabBar setBackgroundImage:tabBackground];

    }

常用委托

//当点击tabBarItem时触发该操作  

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *) viewController {

    NSLog(@"%d", viewController.tabBarItem.tag);

}

通过代理我们可以监测UITabBarController的当前选中viewController的变化,以及moreViewController中对编辑所有viewController的编辑。通过实现下面方法:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;

  该方法用于控制TabBarItem能不能选中,返回NO,将禁止用户点击某一个TabBarItem被选中。但是程序内部还是可以通过直接setSelectedIndex选中该TabBarItem。

  下面这三个方法主要用于监测对moreViewController中对view controller的edit操作

 

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers;

 

- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;

 

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;

 

改变UITabBarController中当前显示的viewController

 1、selectedIndex属性

  通过该属性可以获得当前选中的viewController,设置该属性,可以显示viewControllers中对应的index的viewController。如果当前选中的是MoreViewController的话,该属性获取出来的值是NSNotFound,而且通过该属性也不能设置选中MoreViewController。设置index超出viewControllers的范围,将会被忽略。

  2、selectedViewController属性

  通过该属性可以获取到当前显示的viewController,通过设置该属性可以设置当前选中的viewController,同时更新selectedIndex。可以通过给该属性赋值

tabBarController.moreNavigationController可以选中moreViewController。

  3、viewControllers属性

  设置viewControllers属性也会影响当前选中的viewController,设置该属性时UITabBarController首先会清空所有旧的viewController,然后部署新的viewController,接着尝试重新选中上一次显示的viewController,如果该viewController已经不存在的话,会接着尝试选中index和selectedIndex相同的viewController,如果该index无效的话,则默认选中第一个viewController。

 

 

UITabBarItem

//小红圈提示

barItem1.badgeValue = @"1";

//给选中的状态和未选中的状态指定不同的图片

[item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"] 

   withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];

 

//每个UIController中都有一个navigationItem和一个tabBarItem 一般创建一个相应类型的对象  对其赋值

//系统样式

    UITabBarItem * barItem1 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];

    barItem1.badgeValue = @"1";

    yvCtrl.tabBarItem = barItem1;

//自定义样式

firstController.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"First" image:[UIImage imageNamed:@"img.png"] tag:101]

 

//重写ViewController里的init   在初始化里添加Item

(id)init {  

    if ([super init] != nil) {  

        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"asdfsadf" image:[UIImage imageNamed:@"WWAN5.png"] tag:1];  

        self.tabBarItem = item;  

        [item release];  

    }  

    return self;  

转载于:https://www.cnblogs.com/ldming/archive/2012/06/12/2546725.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值