UITabBarController簡(jiǎn)單介紹

本文轉(zhuǎn)載自:http://www.cnblogs.com/wendingding/p/3775488.html



UITabBarController和UINavigationController類似,UITabBarController也可以輕松的管理多個(gè)控制權(quán),輕松完成控制器之前的轉(zhuǎn)換,經(jīng)典的例子就是qq、微信等應(yīng)用。


UITabBarController的使用

1、使用步驟:

1.1 初始化UITabBarController

1.2 設(shè)置UIWindow的rootViewController為UITabBarController

1.3 創(chuàng)建響應(yīng)的子控制器(viewController)

1.4 把子控制器添加到UITabBarController

2、示例代碼

2.1 建立一個(gè)空的項(xiàng)目File->New->Single View Application, 在Application的代理中編碼,例如AppDelegate.m

額外說明(與本標(biāo)題不相關(guān),但有點(diǎn)聯(lián)系):

如果自定義類需要把Supporting File下的main.m修改為自己的類并在自己的類中繼承UIResponder,聲明UIApplicationDelegate,并設(shè)定一個(gè)UIWindow的對(duì)象(成員變量、屬性)。如

原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


對(duì)應(yīng)的myclass.h為

#import <UIKit/UIKit.h>

@interface myclass : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end


2.2 AppDelegate.m文件內(nèi)實(shí)現(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.初始化一個(gè)tabBar控制器

UITabBarController * tb = [[UITabBarController alloc] init];

//3.設(shè)置控制器為window的根控制器

self.window.rootViewController = tb;

//4.創(chuàng)建子控制器c1

UIViewController * c1 = [[UIViewController alloc] init];

c1.view.backgroundColor = [UIColor grayColor];

//設(shè)定名字

c1.tabBarItem.title =@"首頁(yè)";

//設(shè)定選擇前圖片

c1.tabBarItem.image = [UIImage imageNamed:@"home-page"];

//設(shè)定選擇后圖片

c1.tabBarItem.selectedImage = [UIImage imageNamed:@"home-page_2"];

//設(shè)置圖標(biāo)右上角顯示數(shù)量,在此為寫死,實(shí)際應(yīng)用中應(yīng)根據(jù)取值而定

c1.tabBarItem.badgeValue = @"123";

UILabel * lab1 = [[UUILabel alloc] init];

//設(shè)定背景圖,即使沒有也要設(shè)定。否則無(wú)法顯示(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 = @"第一個(gè)頁(yè)面";

[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 = @"第二個(gè)頁(yè)面";

[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 = @"第三個(gè)頁(yè)面";

[c3.view addSubview:lab3];


//5. 將子控制器添加到UITabBarController中

//第一種方式,逐個(gè)添加:

// [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個(gè)子控制器,那么UITabBar內(nèi)部就有有N個(gè)UITabBarButton作為子控件與之對(duì)應(yīng)。

注意:UITabBarButton在UITabBar中的位置是均分的,UITabBar的高度為49。

在上面的程序中, UITabBarController有3個(gè)子控制器(c1,c2,c3)所以UITabBarButton,UITabBar的結(jié)構(gòu)大致均分。


2. UITabBarButton

UITabBarButton里面顯示什么內(nèi)容,由對(duì)應(yīng)子控制器的tabBarItem屬性來決定


UITabBarItem有一下屬性影響著UITabBarButton的內(nèi)容

標(biāo)題文字

@property(nonatomic, copy) NSString *title;

圖標(biāo)

@property(nonatomic, retain) UIImage *image;

選中時(shí)的圖標(biāo)

@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];

注意:展示的順序和添加的順序一致, 和導(dǎo)航控制器不同,展現(xiàn)在眼前的是第一個(gè)添加的控制器對(duì)應(yīng)的view

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容