標籤:style blog http io color ar os 使用 for
iOS開發UI篇—UITabBarController簡單介紹
一、簡單介紹
UITabBarController和UINavigationController類似,UITabBarController也可以輕鬆地管理多個控制器,輕鬆完成控制器之間的切換,典型的例子就是QQ、等應?。
二、UITabBarController的使用
1.使用步驟:
(1)初始化UITabBarController
(2)設定UIWindow的rootViewController為UITabBarController
(3)建立相應的子控制器(viewcontroller)
(4)把子控制器添加到UITabBarController
2.程式碼範例
建立一個空的檔案,在Application的代理中編碼
TXAppDelegate.m檔案
1 #import "TXAppDelegate.h" 2 3 @implementation TXAppDelegate 4 5 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 6 { 7 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 8 // Override point for customization after application launch. 9 self.window.backgroundColor = [UIColor whiteColor];10 11 // 1.建立tabbar控制器12 UITabBarController *tabbarVc = [[UITabBarController alloc] init];13 14 // 2.設定為window的根控制器15 self.window.rootViewController = tabbarVc;16 17 // 3.添加子控制器18 UIViewController *vc1 = [[UIViewController alloc] init];19 vc1.view.backgroundColor = [UIColor redColor];20 vc1.tabBarItem.title = @"連絡人";21 vc1.tabBarItem.image = [UIImage imageNamed:@"tab_buddy_nor"];22 // [tabbarVc addChildViewController:vc1];23 24 UIViewController *vc2 = [[UIViewController alloc] init];25 vc2.view.backgroundColor = [UIColor blueColor];26 vc2.tabBarItem.title = @"動態";27 vc2.tabBarItem.image = [UIImage imageNamed:@"tab_qworld_nor"];28 // [tabbarVc addChildViewController:vc2];29 30 UIViewController *vc3 = [[UIViewController alloc] init];31 vc3.view.backgroundColor = [UIColor greenColor];32 vc3.tabBarItem.title = @"設定";33 vc3.tabBarItem.image = [UIImage imageNamed:@"tab_me_nor"];34 // [tabbarVc addChildViewController:vc3];35 36 tabbarVc.viewControllers = @[vc1, vc2, vc3];37 38 [self.window makeKeyAndVisible];39 return YES;40 }
實現效果:
三、重要說明
1.UITabBar
下方的工具條稱為UITabBar ,如果UITabBarController有N個子控制器,那麼UITabBar內部就會有N 個UITabBarButton作為子控制項與之對應。
注意:UITabBarButton在UITabBar中得位置是均分的,UITabBar的高度為49。
在上面的程式中,UITabBarController有4個子控制器,所以UITabBar中有4個UITabBarButton,UITabBar的結構?大致如所示:
2.UITabBarButton
UITabBarButton?面顯?什麼內容,由對應子控制器的tabBarItem屬性來決定
[email protected]"訊息"; c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];
3.有兩種方式可以往UITabBarController中添加子控制器
(1)[tb addChildViewController:c1];
(2)[email protected][c1,c2,c3,c4];
注意:展示的順序和添加的順序一致,和導航控制器中不同,展現在眼前的是第一個添加的控制器對應的View。
iOS開發UI篇—UITabBarController簡單介紹