多個(gè)子vc公有一個(gè)navgationController,一個(gè)tabbarController
1.自定義個(gè)navgationController,是window的rootVC,navgationVC的rootVC是TabVC
// 在navigationController.m中
- (void)setRootViewControllerWithNavigation{
_tab = [[UITabBarController alloc]init];
MainViewController *mainViewController = [MainViewController new];
NewsViewController *activeViewController = [NewsViewController new];
ManagerViewController *managerViewController = [ManagerViewController new];
StoreViewController *storeViewController = [StoreViewController new];
MineViewController *mineViewController = [MineViewController new];
mainViewController.title = @"首頁";
activeViewController.title = @"資訊";
managerViewController.title = @"游戲管理";
storeViewController.title = @"商城";
mineViewController.title = @"我的";
_tab.viewControllers = @[mainViewController,activeViewController,storeViewController,mineViewController];
self.navigationController = [[BLNavigationController alloc]initWithRootViewController:_tab];
self.appWindow.rootViewController = self.navigationController;
}
// 在applegate中調(diào)用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[[BLViewControllerManager sharedManager] setRootViewControllerWithNavigation];
return YES;
}
2.不需要navgationBar的在vc的viewWillAppear方法中隱藏,隱藏后所有vc的navgationBar都會(huì)被隱藏,所以需要的vc中的viewWillAppear中設(shè)置為NO;其他屬性也可以在該方法中區(qū)分
- (void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBarHidden = YES;
}
3.給vc設(shè)置navgationItem標(biāo)題時(shí)按普通方法無法顯示標(biāo)題
self.navigationController.navigationItem.title = @"資訊中心";
,所以需要這樣寫
- (void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBarHidden = NO;
self.tabBarController.navigationItem.title = @"資訊中心";
self.tabBarController.navigationItem.rightBarButtonItem = nil;
self.tabBarItem.badgeValue = @"1"; // tabar上的小紅點(diǎn)數(shù)字,類似微信未讀數(shù)
}
每個(gè)子vc有自己的navgationController,共有一個(gè)tabbarController
1 . 自定義TabbarVC,是window的rootVC,TabbarVC的viewcontrollers是navgationVC數(shù)組
// 在自定義的TabbarVC中
-(void)createTabBar{
NewsViewController * news = [[NewsViewController alloc] init];
HeroViewController * hero = [[HeroViewController alloc] init];
FindViewController * find = [[FindViewController alloc] init];
MEViewController * me = [[MEViewController alloc] init];
NSMutableArray * array = [NSMutableArray arrayWithObjects:news,hero,find,me, nil]; // 不要用不可變的給可變的賦值
NSArray * titleArr = @[@"新聞",@"英雄",@"發(fā)現(xiàn)",@"我"];
NSArray * normalArr = @[@"tab_icon_news_normal@2x",@"tab_icon_friend_normal@2x",@"tab_icon_quiz_normal@2x",@"tab_icon_more_normal@2x"];
NSArray * selectedArr = @[@"tab_icon_news_press@2x",@"tab_icon_friend_press@2x",@"tab_icon_quiz_press@2x",@"tab_icon_more_press@2x"];
// 標(biāo)簽欄控制器
for (int i = 0; i< array.count; i ++) {
// 得到每個(gè)視圖控制器
UIViewController * vc = array[i];
// 視圖控制器 --> 導(dǎo)航控制器
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
if (i == 2) {
nav.navigationBarHidden = YES;
}
// 替換數(shù)組(視圖控制器 --> 導(dǎo)航控制器)
[array replaceObjectAtIndex:i withObject:nav];
// 標(biāo)題
vc.title = titleArr[i];
// *渲染模式 : 保證顯示圖片與給定圖片色調(diào)一致
UIImage * normalImage = [UIImage imageNamed:normalArr[i]];
UIImage * selectedImage = [UIImage imageNamed:selectedArr[i]];
// 進(jìn)行渲染后賦值
nav.tabBarItem.image = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
self.viewControllers = array;
}
// 在applegate中調(diào)用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
_window.rootViewController = [[MainTabBarViewController alloc] init];
return YES;
}
每個(gè)子VC的navgation不會(huì)互相影響都可以隨便設(shè)置屬性了