UITabBarController简译及简解

前言:

上一篇文章介绍了UINavigationController,那就不得不讲到UITabBarController,它们经常会搭在一起工作,也是app中最常见的界面。

  • 有兴趣的话欢迎阅读我的上一篇文章UINavigationController简译及简解

一.首先让我们来认识UITabBarController

看一下Xcode里的Description:

The UITabBarController class implements a specialized view controller that manages a radio-style selection interface. This tab bar interface displays tabs at the bottom of the window for selecting between the different modes and for displaying the views for that mode. This class is generally used as-is but may be subclassed in iOS 6 and later.

也是管理视图控制器的工具。

1.看一下官方文档中给出的样子:
UITabBarController简译及简解_第1张图片
The tab bar interface in the Clock application

点击界面下方不同的item会展示相应的ViewController。

2.另外来认识UITabBarController的层级结构
UITabBarController简译及简解_第2张图片
The primary views of a tab bar controller
注意:
  • 你可以使用NavigationController或者普通的ViewController作为tab的根视图控制器。如果将NavigationController作为根视图控制器,那么TabBarController会根据NavigationController调整尺寸以免有重合。
  • tabBar里最多能放5个item,如果你放6个或者以上,那么tabBar会显示前四个,然后再第五个item的位置自动放一个More item来存放多出来的item,你可以点进去选择更多的item。

二.代码学习UITabBarController

创建window

// 1.创建window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 2.设置window背景
self.window.backgroundColor = [UIColor whiteColor];
// 3.使window可见
[self.window makeKeyAndVisible];

创建TabBarController

ViewController *VC = [[ViewController alloc] init];
VC.tabBarItem.title = @"VC";
// item没有放图片

FirstViewController *firstVC = [[FirstViewController alloc] init];
//设置item的title
firstVC.tabBarItem.title = @"first";
// 设置item上显示的图片
firstVC.tabBarItem.image = [[UIImage imageNamed:@"first.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

SecondViewController *secondVC = [[SecondViewController alloc] init];
secondVC.tabBarItem.title = @"second";
secondVC.tabBarItem.image = [[UIImage imageNamed:@"first.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// tabBar里展示NavigationController
UINavigationController *secondNC = [[UINavigationController alloc] initWithRootViewController:secondVC];

ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
thirdVC.tabBarItem.title = @"third";
thirdVC.tabBarItem.image = [[UIImage imageNamed:@"first.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

FourthViewController *fourthVC = [[FourthViewController alloc] init];
fourthVC.tabBarItem.title = @"fourth";
// item的系统自带样式
fourthVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:4];
/* 更多样式,任你来选:
 UITabBarSystemItemMore,
 UITabBarSystemItemFavorites,
 UITabBarSystemItemFeatured,
 UITabBarSystemItemTopRated,
 UITabBarSystemItemRecents,
 UITabBarSystemItemContacts,
 UITabBarSystemItemHistory,
 UITabBarSystemItemBookmarks,
 UITabBarSystemItemSearch,
 UITabBarSystemItemDownloads,
 UITabBarSystemItemMostRecent,
 UITabBarSystemItemMostViewed,
 */

FifthViewController *fifthVC = [[FifthViewController alloc] init];
fifthVC.tabBarItem.title = @"fifth";

UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:VC, firstVC, secondNC, thirdVC, fourthVC, nil];

设置tabBar

// 设置tabBar的tintColor。默认为蓝色
tabBarController.tabBar.tintColor = [UIColor yellowColor];

// 设置tabBar的barTintColor。默认为蓝色
tabBarController.tabBar.barTintColor = [UIColor redColor];

// 设置tabBar的背景图片,会覆盖barTintColor
tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabBar.png"];

为window设置根视图控制器

self.window.rootViewController = tabBarController;

从TabBarController推出NavigationController

// 推出NavigationController
- (void)presentController{
ShowViewController *showVC = [[ShowViewController alloc] init];
showVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;//翻页模式
/* 翻页模式:
 UIModalTransitionStyleCoverVertical,
 UIModalTransitionStyleFlipHorizontal,
 UIModalTransitionStyleCrossDissolve,
 UIModalTransitionStylePartialCurl
 */

UINavigationController  *showNC = [[UINavigationController alloc] initWithRootViewController:showVC];
[self presentViewController:showNC animated:YES completion:^{
    NSLog(@"present finished");
}];
}

将推出的NavigationController返回

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor purpleColor];

// 添加NavigationController的返回按钮
UIBarButtonItem *canael = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissController)];
self.navigationItem.leftBarButtonItem =canael;
}

- (void)dismissController{
[self dismissViewControllerAnimated:YES completion:^{
    NSLog(@"dismiss finished");
}];
}

看到这里想必大家对UITabBarController相当熟悉了吧,而且要比NavigationController的内容少一些。还有一些属性、方法、代理,自己到文档里探索学习吧,看,又是so easy!

  • UITabBar的属性
  • UITabBarItem的属性
  • UITabBarDelegate
  • UITabBarControllerDelegate

后记

小白出手,请多指教。
如言有误,还望斧正!

  • 转载请保留原文地址:http://www.jianshu.com/p/5c7699637b86
  • 有兴趣的读者欢迎关注我的微博:与佳期
  • 本章官方文档:UITabBarController(推荐阅读)

你可能感兴趣的:(UITabBarController简译及简解)