本文轉(zhuǎn)載自:http://www.cnblogs.com/wendingding/p/3775488.html
UITabBarController和UINavigationController類似,UITabBarController也可以輕松的管理多個控制權,輕松完成控制器之前的轉(zhuǎn)換,經(jīng)典的例子就是qq、微信等應用。
UITabBarController的使用
1、使用步驟:
1.1 初始化UITabBarController
1.2 設置UIWindow的rootViewController為UITabBarController
1.3 創(chuàng)建響應的子控制器(viewController)
1.4 把子控制器添加到UITabBarController
2、示例代碼
2.1 建立一個空的項目File->New->Single View Application, 在Application的代理中編碼,例如AppDelegate.m
額外說明(與本標題不相關,但有點聯(lián)系):
如果自定義類需要把Supporting File下的main.m修改為自己的類并在自己的類中繼承UIResponder,聲明UIApplicationDelegate,并設定一個UIWindow的對象(成員變量、屬性)。如
原main:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argh, char * argue[]){
@autoreleasepool{
return UIApplicationMain(argh, argue, nil, NSStringFromClass([AppDelegate class]));
}
}
修改為:
#import <UIKit/UIKit.h>
#import "myclass.h"
int main(int argh, char * argue[]){
@autoreleasepool {
return UIApplicationMain(argh, argue, nil, NSStringFromClass([myclass class]));
}
}
原AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow * window;
@end
對應的myclass.h為
#import <UIKit/UIKit.h>
@interface myclass : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
2.2 AppDelegate.m文件內(nèi)實現(xiàn):
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application : (UIApplication *) application didFinishLaunchingWithOptions : ( NSDictionary * ) launchOptions {
//1.創(chuàng)建window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//2.初始化一個tabBar控制器
UITabBarController * tb = [[UITabBarController alloc] init];
//3.設置控制器為window的根控制器
self.window.rootViewController = tb;
//4.創(chuàng)建子控制器c1
UIViewController * c1 = [[UIViewController alloc] init];
c1.view.backgroundColor = [UIColor grayColor];
//設定名字
c1.tabBarItem.title =@"首頁";
//設定選擇前圖片
c1.tabBarItem.image = [UIImage imageNamed:@"home-page"];
//設定選擇后圖片
c1.tabBarItem.selectedImage = [UIImage imageNamed:@"home-page_2"];
//設置圖標右上角顯示數(shù)量,在此為寫死,實際應用中應根據(jù)取值而定
c1.tabBarItem.badgeValue = @"123";
UILabel * lab1 = [[UUILabel alloc] init];
//設定背景圖,即使沒有也要設定。否則無法顯示(alloc init創(chuàng)建內(nèi)存、CGRectMake創(chuàng)建大小、backgroundColor指定背景或image指定圖片、父類addSubview添加到父類,view缺一不可展現(xiàn)。如果是Controller除外)
lab1.backgroundColor = [UIColor clearColor];
lab1.frame = CGRectMake(100, 100, 100, 20);
lab.text = @"第一個頁面";
[c1.view addSubview:lab];
//4.創(chuàng)建子控制器c2
UIViewController * c2 = [[UIViewController alloc]init];
c2.view.backgroundColor = [UIColor brownColor];
c2.tabBarItem.title = @"客戶錄入";
c2.tabBarItem.image = [UIImage imageNamed:@"User-Icon"];
c2.tabBarItem.selectedImage = [UIImage imageNamed:@"User-Icon_2"];
UILabel * lab2 = [[UILabel alloc] init];
lab2.backgroundColor = [UIColor clearColor];
lab2.frame = CGRectMake(100, 100, 100, 20);
lab2.text = @"第二個頁面";
[c2.view addSubview:lab2];
//4.創(chuàng)建子控制器c3
UIViewController * c3 = [[UIViewController alloc] init];
c3.tabBarItem.title = @"我";
c3.tabBarItem.image = [UIImage imageNamed:@"me"];
c3.tabBarItem.selectedImage = [UIImage imageNamed:@"me_2"];
c3.view.backgroundColor = [UIColor greenColor];
UILabel * lab3 = [[UILabel alloc] init];
lab3.backgroundColor = [UIColor clearColor];
lab3.frame CGRectMake(100, 100, 100, 20);
lab3.text = @"第三個頁面";
[c3.view addSubview:lab3];
//5. 將子控制器添加到UITabBarController中
//第一種方式,逐個添加:
// [tab addChildViewController:c1];
// [tab addChildViewController:c2];
// [tab addChildViewController:c3];
tab.viewControllers = @[c1, c2, c3];
//第二種方式,數(shù)組方式添加
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *) application{
}
- (void)applicationDidEnterBackground:(UIApplication *)application{
}
- (void)applicationWillEnterForeground:(UIApplication *)application{
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
@end
//===重要說明=====重要說明=====重要說明==
1. UITabBar
下方的工具條稱為UITabBar, 如果UITabBarController有N個子控制器,那么UITabBar內(nèi)部就有有N個UITabBarButton作為子控件與之對應。
注意:UITabBarButton在UITabBar中的位置是均分的,UITabBar的高度為49。
在上面的程序中, UITabBarController有3個子控制器(c1,c2,c3)所以UITabBarButton,UITabBar的結(jié)構大致均分。
2. UITabBarButton
UITabBarButton里面顯示什么內(nèi)容,由對應子控制器的tabBarItem屬性來決定
UITabBarItem有一下屬性影響著UITabBarButton的內(nèi)容
標題文字
@property(nonatomic, copy) NSString *title;
圖標
@property(nonatomic, retain) UIImage *image;
選中時的圖標
@property(nonatomic, retain) UIImage *selectedImage;
提醒數(shù)字
@property(nonatomic, copy) NSString *badgeValue;
3. 有兩種方式可以往UITabBarController中添加子控件
3.1 [tb addChildViewController:c1];
3.2 tb.viewControllers = @[c1,c2,c3];
注意:展示的順序和添加的順序一致, 和導航控制器不同,展現(xiàn)在眼前的是第一個添加的控制器對應的view