原鏈接:http://www.cnblogs.com/wendingding/p/3775488.html
原作者:
僅供我個(gè)人收藏學(xué)習(xí),原博主如不同意請(qǐng)聯(lián)系qq651263878進(jìn)行刪除,在此表示感謝以及歉意。**
iOS開發(fā)UI篇—UITabBarController簡(jiǎn)單介紹
一笙纤、簡(jiǎn)單介紹
UITabBarController和UINavigationController類似,UITabBarController也可以輕松地管理多個(gè)控制器,輕松完成控制器之間的切換,典型的例子就是QQ、微信等應(yīng)?。
二荸恕、UITabBarController的使用
1.使用步驟:
(1)初始化UITabBarController
(2)設(shè)置UIWindow的rootViewController為UITabBarController
(3)創(chuàng)建相應(yīng)的子控制器(viewcontroller)
(4)把子控制器添加到UITabBarController
2.代碼示例
新建一個(gè)空的文件,在Application的代理中編碼
YYAppDelegate.m文件
//
// YYAppDelegate.m
// 01-UITabBar控制器基本使用
//
// Created by 孔醫(yī)己 on 14-6-7.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "YYAppDelegate.h"
@implementation YYAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//1.創(chuàng)建Window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//a.初始化一個(gè)tabBar控制器
UITabBarController *tb=[[UITabBarController alloc]init];
//設(shè)置控制器為Window的根控制器
self.window.rootViewController=tb;
//b.創(chuàng)建子控制器
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=@"123";
UIViewController *c2=[[UIViewController alloc]init];
c2.view.backgroundColor=[UIColor brownColor];
c2.tabBarItem.title=@"聯(lián)系人";
c2.tabBarItem.image=[UIImage imageNamed:@"tab_buddy_nor"];
UIViewController *c3=[[UIViewController alloc]init];
c3.tabBarItem.title=@"動(dòng)態(tài)";
c3.tabBarItem.image=[UIImage imageNamed:@"tab_qworld_nor"];
UIViewController *c4=[[UIViewController alloc]init];
c4.tabBarItem.title=@"設(shè)置";
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.設(shè)置Window為主窗口并顯示出來(lái)
[self.window makeKeyAndVisible];
return YES;
}
@end
實(shí)現(xiàn)效果:
三死相、重要說(shuō)明
1.UITabBar
下方的工具條稱為UITabBar 融求,如果UITabBarController有N個(gè)子控制器,那么UITabBar內(nèi)部就會(huì)有N 個(gè)UITabBarButton作為子控件與之對(duì)應(yīng)。
注意:UITabBarButton在UITabBar中得位置是均分的算撮,UITabBar的高度為49生宛。
在上面的程序中,UITabBarController有4個(gè)子控制器,所以UITabBar中有4個(gè)UITabBarButton肮柜,UITabBar的結(jié)構(gòu)?大致如下圖所示:
2.UITabBarButton
UITabBarButton?面顯?什么內(nèi)容,由對(duì)應(yīng)子控制器的tabBarItem屬性來(lái)決定
c1.tabBarItem.title=@"消息"; c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];
3.有兩種方式可以往UITabBarController中添加子控制器
(1)[tb addChildViewController:c1];
(2)tb.viewControllers=@[c1,c2,c3,c4];
注意:展示的順序和添加的順序一致陷舅,和導(dǎo)航控制器中不同,展現(xiàn)在眼前的是第一個(gè)添加的控制器對(duì)應(yīng)的View审洞。