一、源起
小編最近在使用App的時(shí)候侥涵,經(jīng)常遇到UITabBarController為根控制器跳轉(zhuǎn)到另一個(gè)UITabBarController.這種作為可以盡最大可能性的細(xì)分模塊。經(jīng)過幾次實(shí)驗(yàn)和瀏覽相關(guān)App。最終決定了2種方案。
二寸认、實(shí)現(xiàn)關(guān)鍵點(diǎn)
1、實(shí)現(xiàn)UITabBarControllerDelegate
@interface YITabBarController ()
@end
@implementation YITabBarController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.delegate = self;
}
@end
2串慰、實(shí)現(xiàn) -tabBarController:shouldSelectViewController:方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
}
三偏塞、實(shí)現(xiàn)方案
第一種實(shí)現(xiàn) 使用模態(tài)視圖
使用模態(tài)視圖并修改模態(tài)視圖的彈出動(dòng)畫為push的形式
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
//4是泡泡頁面
if ([viewController isEqual:[tabBarController.viewControllers objectAtIndex:4]]) {
SecondTabBarController * vc = [[SecondTabBarController alloc] init];
CATransition * ansition = [CATransition animation];
[ansition setDuration:0.25f];
[ansition setType:kCATransitionMoveIn];
[ansition setSubtype:kCATransitionFromRight];
[[UIApplication sharedApplication].keyWindow.layer addAnimation:ansition forKey:nil];
[self presentViewController:vc animated:NO completion:nil];
return NO;
}
return YES;
}
模態(tài)視圖的返回
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
UIBarButtonItem * leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStyleDone) target:self action:@selector(back)];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
}
- (void)back {
CATransition * ansition = [CATransition animation];
[ansition setDuration:0.25f];
[ansition setType:kCATransitionMoveIn];
[ansition setSubtype:kCATransitionFromLeft];
[[UIApplication sharedApplication].keyWindow.layer addAnimation:ansition forKey:nil];
[self dismissViewControllerAnimated:NO completion:nil];
}
效果如下圖(gif圖,如果不會(huì)動(dòng)請(qǐng)刷新)
弊端
gif圖可能看的不是很清楚邦鲫。
弊端就是在push 或者 pop動(dòng)畫執(zhí)行時(shí)會(huì)看到黑影閃過灸叼,如果要求不高的可以使用這個(gè)神汹。
為了解決這個(gè)問題,我們引出第二種實(shí)現(xiàn)方案.
第二種實(shí)現(xiàn) 使用UINavigationController push的形式
導(dǎo)航欄push的方式
還是先實(shí)現(xiàn)前置條件里的代碼
接著實(shí)現(xiàn)以下代碼
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
//4是泡泡頁面
if ([viewController isEqual:[tabBarController.viewControllers objectAtIndex:4]]) {
SecondTabBarController * vc = [[SecondTabBarController alloc] init];
vc.hidesBottomBarWhenPushed = YES;
UINavigationController * nav = tabBarController.viewControllers[tabBarController.selectedIndex];
[nav pushViewController:vc animated:YES];
return NO;
}
return YES;
}
注意事項(xiàng)
要在第二個(gè)TabBar里隱藏掉最外層的導(dǎo)航欄古今,否則將可能出現(xiàn)2個(gè)導(dǎo)航欄的效果
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
一定要使用 -setNavigationBarHidden:animated: 這個(gè)方法來顯示隱藏導(dǎo)航欄屁魏,并且動(dòng)畫效果不要寫YES或者NO要用animated,否則將會(huì)出現(xiàn)導(dǎo)航欄黑邊閃一下或者手勢(shì)時(shí)出現(xiàn)黑邊的情況捉腥。
效果如下圖(gif圖氓拼,如果不會(huì)動(dòng)請(qǐng)刷新)