UITabBarController

UITabBarController 能够控制一组UIViewController (或者UINavigationController)

UITabBarController 的创建和添加UIViewController步骤:

- (void)viewDidLoad {
    [super viewDidLoad];

    // 创建视图控制器
    OneViewController *oneVC = [[OneViewController alloc] init];
    TwoViewController *twoVC = [[TwoViewController alloc] init];
    ThreeViewController *threeVC = [[ThreeViewController alloc] init];
    FourViewController *fourVC = [[FourViewController alloc] init];
    FiveViewController *fiveVC = [[FiveViewController alloc] init];
    SixViewController *sixVC = [[SixViewController alloc] init];

    // 创建导航控制器
    UINavigationController *oneNav = [[UINavigationController alloc] initWithRootViewController:oneVC];
    UINavigationController *twoNav = [[UINavigationController alloc] initWithRootViewController:twoVC];
    UINavigationController *threeNav = [[UINavigationController alloc] initWithRootViewController:threeVC];
    UINavigationController *fourNav = [[UINavigationController alloc] initWithRootViewController:fourVC];
    UINavigationController *fiveNav = [[UINavigationController alloc] initWithRootViewController:fiveVC];
    UINavigationController *sixNav = [[UINavigationController alloc] initWithRootViewController:sixVC];

    // 显示按钮的标题(tabBarItem)
    oneVC.tabBarItem.title = @"首页";
    twoVC.tabBarItem.title = @"第二页";
    threeVC.tabBarItem.title = @"第三页";
    fourVC.tabBarItem.title = @"第四页";
    fiveVC.tabBarItem.title = @"第五页";
    sixVC.tabBarItem.title = @"第六页";

    // 设置图片
    // 显示图片原有颜色
    // 按原始的图片进行绘制(绘制出来的是原来的颜色)
    oneVC.tabBarItem.image = [[UIImage imageNamed:@"11"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    twoVC.tabBarItem.image = [UIImage imageNamed:@"24-gift"];
    threeVC.tabBarItem.image = [UIImage imageNamed:@"21-skull"];
    fourVC.tabBarItem.image = [UIImage imageNamed:@"61-brightness"];
    fiveVC.tabBarItem.image = [UIImage imageNamed:@"99-umbrella"];
    sixVC.tabBarItem.image = [UIImage imageNamed:@"64-zap"];

    // 设置选中的图片
    oneVC.tabBarItem.selectedImage = [UIImage imageNamed:@"88-beermug"];
    twoVC.tabBarItem.selectedImage = [UIImage imageNamed:@"93-thermometer"];
    threeVC.tabBarItem.selectedImage = [UIImage imageNamed:@"67-tshirt"];
    fourVC.tabBarItem.selectedImage = [UIImage imageNamed:@"101-gameplan"];
    fiveVC.tabBarItem.selectedImage = [UIImage imageNamed:@"108-badge"];
    sixVC.tabBarItem.selectedImage = [UIImage imageNamed:@"109-chicken"];

    // 显示到tabBarControl里面
    // 需要添加到 viewControllers的数组中
    self.viewControllers = @[oneNav, twoNav, threeNav, fourNav, fiveNav, sixNav];

    // 设置bar的颜色
    self.tabBar.barTintColor = [UIColor blueColor];
    // 模块图片的填充色
    self.tabBar.tintColor = [UIColor orangeColor];

    // 设置bar背景图片
    // tabBar的高度 49
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabBar"];

    // 设置默认选中
    self.selectedIndex = 2;

    // tabBarItem上的 红色提示按钮
    twoVC.tabBarItem.badgeValue = @"99+";

    // 设置代理
    self.delegate = self;




    // 释放
    [oneVC release];
    [twoVC release];
    [threeVC release];
    [fourVC release];
    [fiveVC release];
    [sixVC release];

    [oneNav release];
    [twoNav release];
    [threeNav release];
    [fourNav release];
    [fiveNav release];
    [sixNav release];
}

协议方法

#pragma mark -- 代理方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    // 可以指定哪个控制器不让点击
    // 先取出 不让点击的控制器
    UINavigationController *nav = self.viewControllers[3];
    // 如果选中的控制器是你不想让用户点击的 那么返回NO
    if (viewController == nav) {
        return NO;
    }else{
        return YES;
    }
}

// 选中页面时触发的方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    // 打印选中的索引
    NSLog(@"%ld",self.selectedIndex);
    // 选中时 把红点去除
    viewController.tabBarItem.badgeValue = nil;
}

// 控制more的代理方法
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers
{
    NSLog(@"将要开始编辑more");
}

- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed
{
    NSLog(@"将要结束编辑more");
}

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed
{
    NSLog(@"已经结束编辑more");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值