目錄:
1荧止、利用ViewController中間過渡
2屹电、直接設(shè)置UITabBarController的數(shù)組
比較:相對來說第2中方式較方便,設(shè)置內(nèi)容跃巡、標(biāo)題等比較清晰明確危号,第1中方式中設(shè)置標(biāo)題等內(nèi)容時容易搞混,相對第2中方式?jīng)]有較大的優(yōu)勢素邪。
1外莲、利用ViewController中間過渡
AppDelegate中代碼設(shè)置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
return YES;
}
ViewController中代碼設(shè)置
//初始化要使用的三個VC
FirstVc *first = [[FirstVc alloc] init];
SecondVc *second = [[SecondVc alloc] init];
ThirdVc *third = [[ThirdVc alloc] init];
//初始化三個nav
UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:first];
firstNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"111" image:nil tag:1];
UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:second];
secondNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:nil tag:1];
UINavigationController *thirdNav = [[UINavigationController alloc] initWithRootViewController:third];
thirdNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"333" image:nil tag:1];
//初始化UITabBarController
self.tabBarCont = [[UITabBarController alloc] init];
self.tabBarCont.viewControllers = @[firstNav,secondNav,thirdNav];
self.tabBarCont.selectedIndex = 0;
self.tabBarCont.delegate = self;
self.tabBarCont.view.frame = self.view.frame;
[self.view addSubview:self.tabBarCont.view];
各個VC中的設(shè)置
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)viewWillAppear:(BOOL)animated
{
self.title = @"first";//此設(shè)置改變nav的標(biāo)題和底部item的標(biāo)題
self.navigationController.title = @"111";//item標(biāo)題
}
2、直接設(shè)置UITabBarController的數(shù)組
AppDelegate中設(shè)置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//初始化
FirstVc *first = [[FirstVc alloc] init];
SecondVc *second = [[SecondVc alloc] init];
ThirdVc *third = [[ThirdVc alloc] init];
//創(chuàng)建標(biāo)簽欄控制器
tabBarControl = [UITabBarController new];
//放入標(biāo)簽欄中
tabBarControl.viewControllers = @[first,second,third];
//創(chuàng)建導(dǎo)航欄控制器兔朦,并指定他的根視圖控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tabBarControl];
//添加中間的按鈕
//缺點(diǎn):1偷线、按鈕超過bottom的部分點(diǎn)擊無響應(yīng) 2、點(diǎn)擊除按鈕外的中間部分時也展示中間item的頁面
//3個item是可能范圍太大沽甥,如果調(diào)整item數(shù)量声邦,缺點(diǎn)2應(yīng)該影響不大。
UIButton *btn = [[UIButton alloc] init];
btn.backgroundColor = [UIColor redColor];
[btn setFrame:CGRectMake(130, -12, 60, 60)];
btn.clipsToBounds = YES;
btn.layer.cornerRadius = 30;
[btn addTarget:self action:@selector(clickCenterButton) forControlEvents:UIControlEventTouchUpInside];
[tabBarControl.tabBar addSubview:btn];
//指定應(yīng)用的跟視圖控制器
self.window.rootViewController = nav;
return YES;
}
各個VC中的設(shè)置
- (instancetype)init
{
self = [super init];
if (self)
{
self.title = @"111";//此處的title是item的標(biāo)題
//設(shè)置圖標(biāo)的默認(rèn)圖片和選中后圖片
self.tabBarItem.image = [[UIImage imageNamed:@"tabbar_mainframe@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_mainframeHL@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//未讀消息數(shù)量(右上角標(biāo)識)
[self.tabBarItem setBadgeValue:@"18"];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)viewWillAppear:(BOOL)animated
{
self.tabBarController.navigationItem.title = @"first";//共同使用一個navigation摆舟,要在此方法中設(shè)置名稱
//設(shè)置右邊導(dǎo)航按鈕
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(tapAdd)];
self.tabBarController.navigationItem.rightBarButtonItem = rightItem;
}
- (void)tapAdd
{
//此處跳轉(zhuǎn)頁面操作
//或者彈出頁面
}