美文网首页Swift初体验
UITabBarController简译及简解

UITabBarController简译及简解

作者: 与佳期 | 来源:发表于2015-10-12 16:11 被阅读1915次

前言:

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

一.首先让我们来认识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.看一下官方文档中给出的样子: The tab bar interface in the Clock application

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

2.另外来认识UITabBarController的层级结构
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

后记

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

相关文章

  • UITabBarController简译及简解

    前言: 上一篇文章介绍了UINavigationController,那就不得不讲到UITabBarControl...

  • UINavigationController简译及简解

    前言: 这是一篇小白的技术博客,写下来一是为了整理,二是为了分享。就从UI基础开始写起吧。另,初次执笔,难免言辞羞...

  • 心经注音及简译

    日更第三天因为长途坐车耽误,使用复活卡,仍然卡顿,继续摘抄心经及读音和简译。摘自百度文库…… guān zì zà...

  • Profile

    写在前面: 心语每日一摘, 英语每日一译。 不忘初心兴趣, 简悟简译简书。

  • startActivity简解

    1.概要 startActivity启动一个activity的源码分析,涉及概念较多,比如binder,aidl跨...

  • 极致简解

    十年磨一剑,那些顶级高手,都是在某些点上做到了极致。 极致,首先一定是聚焦,其次才是深耕。 昨天晚上,供货商的张总...

  • 【我在悦读】《完全写作指南》

    【我在悦读】Ami 书名:《完全写作指南》 作者:[美] 劳拉 布朗 (袁婧 译) 篇目:第一 二章 及求职信 简...

  • 寻找 In search of

    【IN SEARCH OF...】 Michele Catalano作 / 简永 译 They searched ...

  • 简婶揭秘019 —— 这三个原则你不知道,在互联网上就只能被骗

    许久未推荐简书好文,不知道大家伙有没有想念简婶婶?简婶正在发起 简书 · 产品周译挑战计划 ,欢迎大家投稿。 01...

  • Raft Paper 简译

      本文是对 Raft Paper 核心部分的意译,不包括原文中的如下章节:<3 Paxos 的优缺点论述>、<4...

网友评论

  • 菜鸟晋升路:楼主 你的demo怎么不存在了?
    与佳期:@菜鸟晋升路 代码很简单,你可以自己建一个工程照着文中的代码敲一遍,会更有收获的

本文标题:UITabBarController简译及简解

本文链接:https://www.haomeiwen.com/subject/hwhocttx.html