iOS UITabBarController基本使用

原文:iOS UITabBarController基本使用
自定義設(shè)置:ios ~ 設(shè)置UITabBarController

底部TabBar可以說(shuō)每個(gè)App的標(biāo)配了,大部分一個(gè)Tab就是App的一個(gè)模塊的功能首頁(yè)。在Android中帕翻,底部TabBar一般用RadioGroup和RadioButton來(lái)自定義,就是單選組和單選按鈕称勋。而iOS上則提供了UITabBarController。Android上的TabBar切換一般為Fragment涯竟,而iOS上的TabBar切換是切換ViewController赡鲜。

Android直觀感覺(jué)就是給你一堆控件,你自己自由發(fā)揮庐船,而iOS則是封裝好了給你讓你直接用银酬,還把建議給你,按照他來(lái)做就好了筐钟。

UITabBarController創(chuàng)建和基本配置

UITabBarController就是多個(gè)ViewController的容器揩瞪,他們之間的層級(jí)是平行的,它會(huì)在底部添加一個(gè)TabBar的UIView篓冲,通過(guò)點(diǎn)擊TabBar上的按鈕tabBarItem來(lái)切換對(duì)應(yīng)的ViewController李破。

  • UITabBarController一般配合UINavigationController來(lái)使用,這樣可以實(shí)現(xiàn)多Tab壹将,多棧跳轉(zhuǎn)頁(yè)面視圖嗤攻。

分為4步走:

  1. 創(chuàng)建Tab所屬的ViewController。
  2. 創(chuàng)建一個(gè)數(shù)組诽俯,將控制器放到數(shù)組中妇菱。
  3. 創(chuàng)建UITabBarController,將控制器數(shù)組設(shè)置給它。
  4. 將UITabBarController設(shè)置為Window的rootViewController闯团。
  5. 顯示W(wǎng)indow辛臊。
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    //1.創(chuàng)建Tab所屬的ViewController
    //首頁(yè)
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:homeVC];
    homeNav.navigationBar.translucent = NO;

    //工作
    WorkViewController *workVC = [[WorkViewController alloc] init];
    UINavigationController *workNav = [[UINavigationController alloc] initWithRootViewController:workVC];
    workNav.navigationBar.translucent = NO;

    //通知
    NoticeViewController *noticeVC = [[NoticeViewController alloc] init];
    UINavigationController *noticeNav = [[UINavigationController alloc] initWithRootViewController:noticeVC];
    noticeNav.navigationBar.translucent = NO;

    //我的
    MineViewController *mineVC = [[MineViewController alloc] init];
    UINavigationController *mineNav = [[UINavigationController alloc] initWithRootViewController:mineVC];
    mineNav.navigationBar.translucent = NO;

    //2、創(chuàng)建一個(gè)數(shù)組偷俭,放置多有控制器
    NSArray *vcArray = [NSArray arrayWithObjects:homeNav, workNav, noticeNav, mineNav, nil];

    //3浪讳、創(chuàng)建UITabBarController缰盏,將控制器數(shù)組設(shè)置給UITabBarController
    UITabBarController *tabBarVC = [[UITabBarController alloc] init];
    //設(shè)置多個(gè)Tab的ViewController到TabBarViewController
    tabBarVC.viewControllers = vcArray;

    //4涌萤、將UITabBarController設(shè)置為Window的RootViewController
    self.window.rootViewController = tabBarVC;
    //顯示W(wǎng)indow
    [self.window makeKeyAndVisible];
    return YES;
}

@end

TabBar樣式和紅點(diǎn)氣泡

經(jīng)過(guò)上面的設(shè)置,3個(gè)Tab的ViewController能顯示也能切換口猜。但是TabBar上沒(méi)有控件顯示负溪,TabBar的控件通過(guò)UITabBarItem來(lái)設(shè)置,每個(gè)ViewController都有一個(gè)self.tabBarItem屬性济炎,通過(guò)設(shè)置一個(gè)屬性來(lái)設(shè)置TabBar上的Tab川抡。下面演示的方法都在ViewContoller中使用。

  • 以Tab文字和非選中须尚、選中圖片來(lái)創(chuàng)建Tab崖堤,按鈕的UIImage默認(rèn)會(huì)受TabBar的tintColor屬性影響而著色,一般希望不跟隨tintColor屬性耐床,會(huì)使用imageWithRenderingMode設(shè)置UIImageRenderingModeAlwaysOriginal來(lái)保持圖片原有的顏色密幔。
//根據(jù)標(biāo)題、非選中圖片撩轰、選中圖片來(lái)構(gòu)建一個(gè)Tab
UITabBarItem *tabItem = [[UITabBarItem alloc] initWithTitle:@"首頁(yè)" image:[[UIImage imageNamed:@"home_icon_home_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"home_icon_home_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
//設(shè)置Tab
self.tabBarItem = tabItem;

  • 以系統(tǒng)圖標(biāo)風(fēng)格來(lái)構(gòu)建一個(gè)Tab
UITabBarItem* tabItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
//設(shè)置Tab
self.tabBarItem = tabItem;

  • 設(shè)置未讀紅點(diǎn)
//設(shè)置未讀數(shù)量字符串
tabItem.badgeValue = @"99+";

TabBar風(fēng)格

  • 設(shè)置TabBar是否透明胯甩,默認(rèn)為透明
tabBarVC.tabBar.translucent = NO;
  • 設(shè)置TabBar的顏色
tabBarVC.tabBar.barTintColor = [UIColor redColor];
  • 設(shè)置TabBar的風(fēng)格顏色,會(huì)將圖片和文字都設(shè)置堪嫂,除非圖片設(shè)置了imageWithRenderingMode為UIImageRenderingModeAlwaysOriginal偎箫。
tabBarVC.tabBar.tintColor = [UIColor redColor];

設(shè)置tabBar:

TabBar

/* 全局設(shè)置 */

// TabBar背景顏色

[UITabBar appearance].barTintColor = [UIColor whiteColor];

/* 單獨(dú)設(shè)置 */

// TabBar背景顏色

self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];


TabBar圖標(biāo)顏色

不用寫(xiě)亂七八糟的代碼,直接到 Assets.xcassets 里把圖片的屬性 Render 設(shè)置為 Original Image 就可以讓顏色按照?qǐng)D片的來(lái)皆串,而不會(huì)選中變藍(lán)了淹办。

WeChat9317557dea89f807db8fdde6ee45f3f3.png

UITabBarController的Api和代理方法

  • 設(shè)置默認(rèn)選擇控制器的索引
//選中第三個(gè)Tab
tabBarVC.selectedIndex = 2;
  • 獲取當(dāng)前選中的Tab的索引
NSUInteger curSelectIndex = tabBarVC.selectedIndex;
NSLog(@"當(dāng)前選中的Tab角標(biāo):%lu", curSelectIndex);
  • 獲取當(dāng)前選中的Tab的ViewController
UIViewController *curSelectVC = tabBarVC.selectedViewController;
  • 設(shè)置UITabBarController代理
//1.遵循協(xié)議UITabBarControllerDelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

@end

//2.設(shè)置代理
tabBarVC.delegate = self;

/**
 * 當(dāng)選中控制器時(shí)回調(diào)
 */
- (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    //選中的ViewController實(shí)例
    UIViewController *selectVC = tabBarController.selectedViewController;
    NSLog(@"選中的index: %zd, 選中的ViewController: %@", tabBarController.selectedIndex, selectVC);
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末恶复,一起剝皮案震驚了整個(gè)濱河市怜森,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌寂玲,老刑警劉巖塔插,帶你破解...
    沈念sama閱讀 212,718評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異拓哟,居然都是意外死亡想许,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)流纹,“玉大人糜烹,你說(shuō)我怎么就攤上這事∈” “怎么了疮蹦?”我有些...
    開(kāi)封第一講書(shū)人閱讀 158,207評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)茸炒。 經(jīng)常有香客問(wèn)我愕乎,道長(zhǎng),這世上最難降的妖魔是什么壁公? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,755評(píng)論 1 284
  • 正文 為了忘掉前任感论,我火速辦了婚禮,結(jié)果婚禮上紊册,老公的妹妹穿的比我還像新娘比肄。我一直安慰自己,他們只是感情好囊陡,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,862評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布芳绩。 她就那樣靜靜地躺著,像睡著了一般撞反。 火紅的嫁衣襯著肌膚如雪妥色。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 50,050評(píng)論 1 291
  • 那天痢畜,我揣著相機(jī)與錄音垛膝,去河邊找鬼。 笑死丁稀,一個(gè)胖子當(dāng)著我的面吹牛吼拥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播线衫,決...
    沈念sama閱讀 39,136評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼凿可,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了授账?” 一聲冷哼從身側(cè)響起枯跑,我...
    開(kāi)封第一講書(shū)人閱讀 37,882評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎白热,沒(méi)想到半個(gè)月后敛助,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,330評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡屋确,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,651評(píng)論 2 327
  • 正文 我和宋清朗相戀三年纳击,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了续扔。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,789評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡焕数,死狀恐怖纱昧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情堡赔,我是刑警寧澤识脆,帶...
    沈念sama閱讀 34,477評(píng)論 4 333
  • 正文 年R本政府宣布,位于F島的核電站善已,受9級(jí)特大地震影響灼捂,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜雕拼,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,135評(píng)論 3 317
  • 文/蒙蒙 一纵东、第九天 我趴在偏房一處隱蔽的房頂上張望粘招。 院中可真熱鬧啥寇,春花似錦、人聲如沸洒扎。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,864評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)袍冷。三九已至磷醋,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間胡诗,已是汗流浹背邓线。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,099評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留煌恢,地道東北人骇陈。 一個(gè)月前我還...
    沈念sama閱讀 46,598評(píng)論 2 362
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像瑰抵,于是被迫代替她去往敵國(guó)和親你雌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,697評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容