35.UITabBarController(标签视图控制器)

这里写图片描述

UITabBarController继承于UIViewController

这里写图片描述

===================================================

在UITabBarController.m文件中

创建6个视图控制器

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];

设置标题

oneVC.tabBarItem.title = @”首页”;

twoVC.tabBarItem.title = @”第二页”;

threeVC.tabBarItem.title = @”第三页”;

fourVC.tabBarItem.title = @”第四页”;

fiveVC.tabBarItem.title = @”第五页”;

sixVC.tabBarItem.title = @”第六页”;

设置图片

显示图片原有颜色

//UIImageRenderingModeAlwaysOriginal

// 按原始的图片进行绘制(绘制出来是原来的颜色)

oneVC.tabBarItem.image = [[UIImage imageNamed:@”26-bandaid”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

twoVC.tabBarItem.image = [[UIImage imageNamed:@”109-chicken”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

threeVC.tabBarItem.image = [[UIImage imageNamed:@”11-clock”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

fourVC.tabBarItem.image = [[UIImage imageNamed:@”11”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

fiveVC.tabBarItem.image = [[UIImage imageNamed:@”110-bug”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

sixVC.tabBarItem.image = [[UIImage imageNamed:@”04-squiggle”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

设置选中的图片

oneVC.tabBarItem.selectedImage = [UIImage imageNamed:@”108-badge”];

twoVC.tabBarItem.selectedImage = [UIImage imageNamed:@”108-badge”];

threeVC.tabBarItem.selectedImage = [UIImage imageNamed:@”108-badge”];

fourVC.tabBarItem.selectedImage = [UIImage imageNamed:@”108-badge”];

fiveVC.tabBarItem.selectedImage = [UIImage imageNamed:@”108-badge”];

sixVC.tabBarItem.selectedImage = [UIImage imageNamed:@”108-badge”];

显示到tabBarController

需要添加到viewControllers数组中

self.viewControllers = @[oneNav,twoNav,threeNav,fourNav,fiveNav,sixNav];

设置bar的颜色

self.tabBar.barTintColor = [UIColor blueColor];

设置bar的填充色

self.tabBar.tintColor = [UIColor greenColor];

设置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 – 代理方法

1.可以指定哪个控制器不让点 是否允许点击

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

{

可以指定哪个控制不让点击

先取出 不让点击的控制器

UINavigationController *nav = self.viewControllers[3];

如果选中的控制器是你不想让用户点击

那么返回NO

if (nav == viewController) {

return NO;

}

return YES;

}

2.选中页面时触发的方法

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

{

打印选中的索引

NSLog(@”%ld”,self.selectedIndex);

选中时把红点去掉

if (viewController.tabBarItem.badgeValue != nil) {

viewController.tabBarItem.badgeValue = nil;

};

}

3.控制more的代理方法

- (void)tabBarController:(UITabBarController )tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController > *)viewControllers

{

NSLog(@”将要开始编辑more”);

}

4.将要结束编辑more

- (void)tabBarController:(UITabBarController )tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController > *)viewControllers changed:(BOOL)changed

{

NSLog(@”将要结束编辑”);

}

5.已经结束编辑more

- (void)tabBarController:(UITabBarController )tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController > *)viewControllers changed:(BOOL)changed

{

NSLog(@”已经结束编辑more”);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值