iOS開發UI篇—UITabBarController簡單介紹


 最新博文發布地址 花田半畝http://wendingding.com/

iOS開發UI篇—UITabBarController簡單介紹

一、簡單介紹

UITabBarController和UINavigationController類似,UITabBarController也可以輕松地管理多個控制器,輕松完成控制器之間的切換,典型的例子就是QQ、微信等應⽤。

 

二、UITabBarController的使用

1.使用步驟:

(1)初始化UITabBarController

(2)設置UIWindow的rootViewController為UITabBarController

(3)創建相應的子控制器(viewcontroller)

(4)把子控制器添加到UITabBarController

2.代碼示例

新建一個空的文件,在Application的代理中編碼

YYAppDelegate.m文件

 1 //
 2 //  YYAppDelegate.m
 3 //  01-UITabBar控制器基本使用
 4 //
 5 //  Created by 孔醫己 on 14-6-7.
 6 //  Copyright (c) 2014年 itcast. All rights reserved.
 7 //
 8 
 9 #import "YYAppDelegate.h"
10 
11 @implementation YYAppDelegate
12 
13 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
14 {
15     //1.創建Window
16     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
17     self.window.backgroundColor = [UIColor whiteColor];
18     
19     //a.初始化一個tabBar控制器
20     UITabBarController *tb=[[UITabBarController alloc]init];
21     //設置控制器為Window的根控制器
22     self.window.rootViewController=tb;
23     
24     //b.創建子控制器
25     UIViewController *c1=[[UIViewController alloc]init];
26     c1.view.backgroundColor=[UIColor grayColor];
27     c1.view.backgroundColor=[UIColor greenColor];
28     c1.tabBarItem.title=@"消息";
29     c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];
30     c1.tabBarItem.badgeValue=@"123";
31     
32     UIViewController *c2=[[UIViewController alloc]init];
33     c2.view.backgroundColor=[UIColor brownColor];
34     c2.tabBarItem.title=@"聯系人";
35     c2.tabBarItem.image=[UIImage imageNamed:@"tab_buddy_nor"];
36     
37     UIViewController *c3=[[UIViewController alloc]init];
38     c3.tabBarItem.title=@"動態";
39     c3.tabBarItem.image=[UIImage imageNamed:@"tab_qworld_nor"];
40     
41     UIViewController *c4=[[UIViewController alloc]init];
42     c4.tabBarItem.title=@"設置";
43     c4.tabBarItem.image=[UIImage imageNamed:@"tab_me_nor"];
44    
45     
46     //c.添加子控制器到ITabBarController中
47     //c.1第一種方式
48 //    [tb addChildViewController:c1];
49 //    [tb addChildViewController:c2];
50     
51     //c.2第二種方式
52     tb.viewControllers=@[c1,c2,c3,c4];
53     
54     
55     //2.設置Window為主窗口並顯示出來
56     [self.window makeKeyAndVisible];
57     return YES;
58 }
59 
60 @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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM