iOS UI-标签控制器(UITabBarController)
#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor]; //创建子控制器
FirstViewController *first = [[FirstViewController alloc] init];
first.tabBarItem.title = @"主页";
first.tabBarItem.image = [UIImage imageNamed:@"home.png"];
first.tabBarItem.badgeValue = @""; SecondViewController *second = [[SecondViewController alloc] init];
second.tabBarItem.title = @"设置";
second.tabBarItem.image = [UIImage imageNamed:@"setting.png"]; //创建标签控制器
UITabBarController *tabCtr = [[UITabBarController alloc] init]; NSArray *viewControllerArr = [NSArray arrayWithObjects:first,second, nil]; tabCtr.viewControllers = viewControllerArr; self.window.rootViewController = tabCtr; [self.window makeKeyAndVisible]; return YES;
} #import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width-, )];
label.backgroundColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"第一个视图";
[self.view addSubview:label]; self.view.backgroundColor = [UIColor cyanColor];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end #import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width-, )];
label.backgroundColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"第二个视图";
[self.view addSubview:label]; self.view.backgroundColor = [UIColor purpleColor];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
一、简单介绍
UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例子就是QQ、微信等应⽤。
二、UITabBarController的使用
1.使用步骤:
(1)初始化UITabBarController
(2)设置UIWindow的rootViewController为UITabBarController
(3)创建相应的子控制器(viewcontroller)
(4)把子控制器添加到UITabBarController
2.代码示例
新建一个空的文件,在Application的代理中编码
YYAppDelegate.m文件
//
// YYAppDelegate.m
// 01-UITabBar控制器基本使用
//
// Created by 孔医己 on 14-6-7.
// Copyright (c) 2014年 itcast. All rights reserved.
// #import "YYAppDelegate.h" @implementation YYAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//1.创建Window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor]; //a.初始化一个tabBar控制器
UITabBarController *tb=[[UITabBarController alloc]init];
//设置控制器为Window的根控制器
self.window.rootViewController=tb; //b.创建子控制器
UIViewController *c1=[[UIViewController alloc]init];
c1.view.backgroundColor=[UIColor grayColor];
c1.view.backgroundColor=[UIColor greenColor];
c1.tabBarItem.title=@"消息";
c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];
c1.tabBarItem.badgeValue=@""; UIViewController *c2=[[UIViewController alloc]init];
c2.view.backgroundColor=[UIColor brownColor];
c2.tabBarItem.title=@"联系人";
c2.tabBarItem.image=[UIImage imageNamed:@"tab_buddy_nor"]; UIViewController *c3=[[UIViewController alloc]init];
c3.tabBarItem.title=@"动态";
c3.tabBarItem.image=[UIImage imageNamed:@"tab_qworld_nor"]; UIViewController *c4=[[UIViewController alloc]init];
c4.tabBarItem.title=@"设置";
c4.tabBarItem.image=[UIImage imageNamed:@"tab_me_nor"]; //c.添加子控制器到ITabBarController中
//c.1第一种方式
// [tb addChildViewController:c1];
// [tb addChildViewController:c2]; //c.2第二种方式
tb.viewControllers=@[c1,c2,c3,c4]; //2.设置Window为主窗口并显示出来
[self.window makeKeyAndVisible];
return YES;
} @end
实现效果:
三、重要说明
1.UITabBar
下方的工具条称为UITabBar ,如果UITabBarController有N个子控制器,那么UITabBar内部就会有N 个UITabBarButton作为子控件与之对应。
注意:UITabBarButton在UITabBar中得位置是均分的,UITabBar的高度为49。
在上面的程序中,UITabBarController有4个子控制器,所以UITabBar中有4个UITabBarButton,UITabBar的结构⼤大致如下图所示:
2.UITabBarButton
UITabBarButton⾥面显⽰什么内容,由对应子控制器的tabBarItem属性来决定
c1.tabBarItem.title=@"消息";
c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];
3.有两种方式可以往UITabBarController中添加子控制器
(1)[tb addChildViewController:c1];
(2)tb.viewControllers=@[c1,c2,c3,c4];
注意:展示的顺序和添加的顺序一致,和导航控制器中不同,展现在眼前的是第一个添加的控制器对应的View。



//
// YYbaseViewController.m
// 02-uitabbarcontroller
//
// Created by 孔医己 on 14-6-8.
// Copyright (c) 2014年 itcast. All rights reserved.
// #import "YYbaseViewController.h" @interface YYbaseViewController () @end @implementation YYbaseViewController // 当控制器的view加载完毕就调用
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@ - 控制器的view加载完毕", [self class]);
} // 控制器即将显示的时候调用
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
NSLog(@"%@ - 控制器即将显示", [self class]);
} // 控制器完全显示的时候调用
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"%@ - 控制器完全显示", [self class]);
} // 控制器即将消失的时候调用
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog(@"%@ - 控制器即将消失", [self class]);
}
// 控制器完全消失的时候调用
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSLog(@"%@ - 控制器完全消失", [self class]);
} - (void)viewWillUnload
{
[super viewWillUnload];
NSLog(@"%@ - view即将被销毁", [self class]);
} - (void)viewDidUnload
{
[super viewDidUnload];
NSLog(@"%@ - view完全被销毁", [self class]);
} - (void)dealloc
{
NSLog(@"%@", [self class]);
} @end



- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
UITabBarController *tb=(UITabBarController*)self.window.rootViewController;
NSLog(@"%@",NSStringFromCGRect(tb.tabBar.frame));
}
iOS UI-标签控制器(UITabBarController)的更多相关文章
- 标签控制器 UITabBarController
UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换. #import ...
- 标签控制器,UITabBarController
注意: 1.tabbar高度不可设置,可通过_tabbar.tabbar.frame设置tabbar的位置 2.tabbar不同页面添加同一个视图后其那面添加的不起作用,只有最后一个才具有所添加的仕途 ...
- iOS开发UI篇—控制器的创建
iOS开发UI篇—控制器的创建 说明:控制器有三种创建方式,下面一一进行说明. 一.第一种创建方式(使用代码直接创建) 1.创建一个空的IOS项目. 2.为项目添加一个控制器类. 3.直接在代理方法中 ...
- iOS开发UI篇—控制器的View的创建
iOS开发UI篇—控制器的View的创建 一.6种创建控制器View的方式 #import "NJAppDelegate.h" #import "NJViewContro ...
- 标签视图控制器UITabBarController
标签视图控制器 UITabBarController FirstViewController*first = [[FirstViewController alloc] init]; //创建一个UIT ...
- 【iOS开发-30】UITabBarController的几种代理方法以及结合NSUserDefaults还原上次退出时被选中视图控制器和视图控制器的顺序
一.UITabBarController的几种代理方法 在AppDelegate.h中加入一个协议<UITabBarControllerDelegate>.然后再AppDelegate.m ...
- 集合视图控制器(CollectionViewController) 、 标签控制器(TabBarController) 、 高级控件介绍
1 创建集合视图,设置相关属性以满足要求 1.1 问题 集合视图控制器UIConllectionViewController是一个展示大量数据的控制器,系统默认管理着一个集合视图UICollectio ...
- iOS学习28之UITabBarController
1. 标签视图控制器 -- UITabBarController 视图(UIView) ---> 图层 ---> 子视图 视图控制器(UIViewController) ---> 管 ...
- iOS中多控制器的使用
通常情况下,一个app由多个控制器组成,当app中有多个控制器的时候,我们就需要对这些控制器进行管理. 在开发过程中,当有多个View时,可以用一个大的view去管理多个小的view,控制器也是如此, ...
随机推荐
- Python Web学习笔记之并发编程IO模型
了解新知识之前需要知道的一些知识 同步(synchronous):一个进程在执行某个任务时,另外一个进程必须等待其执行完毕,才能继续执行 #所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调 ...
- 20145303刘俊谦 《网络对抗》Exp9 Web安全基础实践
20145303刘俊谦 <网络对抗>Exp9 Web安全基础实践 基础问题回答 1.SQL注入原理,如何防御 SQL注入 就是通过把SQL命令插入到"Web表单递交"或 ...
- 20145334赵文豪《网络对抗》shellcode注入&Return-to-libc攻击深入
Shellcode注入 shellcode实际是一段代码,但却作为数据发送给受攻击服务器,将代码存储到对方的堆栈中,并将堆栈的返回地址利用缓冲区溢出,覆盖成为指向 shellcode 的地址 下载安装 ...
- STM32系统时钟为什么没有定义呢
对于使用3.5版本库开发的STM32学习者 有时候不清楚为什么没有时钟定义 那么我们就简单的讲解下吧: 1,函数从启动文件开始运行(汇编文件) 2,若是hd.s 请看151行LDR R0, = ...
- jquery hover最后解决 - 不再疑惑 - 例子在这里
hover具有动画累计的bug, 可以使用 stop 或 filter(:not(:animated))来消除, 但是, 即使这样, 当鼠标反复滑入或滑出的时候, 虽然没有动画累计的问题, 但是 下面 ...
- 【修改编码】修改tomcat默认的编码方式
默认情况下,tomcat使用的的编码方式:iso8859-1 修改tomcat下的conf/server.xml文件 找到如下代码: <Connector port="8080& ...
- JavaScript紧凑学习
JavaScript紧凑学习 windows本地,调用命令行: win键+R 键入cmd , (cmd是Command 命令行 简称) 目录是C盘下的 C:\Users\Administrator&g ...
- MYSQL语句:创建、授权、查询、修改、统计分析等 二 用户的创建、权限设置、删除
接着上面一的内容 4.设置更改用户密码 命令格式:SET PASSWORD FOR 'username'@'host'=PASSWORD('newpassword'); 如果是当前登录用户用:SET ...
- python input选择
例1 import sys #声明字符串数组并初始化 newspaper=['1.北京晚报','2.作家文摘','3.参考消息', \ '4.证券报','5.不需要'] #字符串数组的输出 ): pr ...
- API接口自动化之1 常见的http请求
常见的HTTP请求 就我所想到的,目前如果想做http/https请求接口自动户化,要处理的一些区别 get请求post请求 httphttps 单个值键值对键值对的搭配方式 POST请求体,后续讲一 ...