Swift初体验

UITabBarController简译及简解

2015-10-12  本文已影响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
注意:

二.代码学习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!

后记

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

上一篇 下一篇

猜你喜欢

热点阅读