原文: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步走:
- 創(chuàng)建Tab所屬的ViewController。
- 創(chuàng)建一個(gè)數(shù)組诽俯,將控制器放到數(shù)組中妇菱。
- 創(chuàng)建UITabBarController,將控制器數(shù)組設(shè)置給它。
- 將UITabBarController設(shè)置為Window的rootViewController闯团。
- 顯示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)了淹办。
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);
}