想要設(shè)置一個同時使用UINavigationController和UITabBarController的結(jié)構(gòu)娘纷,我在AppDelegate.m中這樣寫:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//自定義的UITabBarController
myTabBarController * myTabBar = [[myTabBarController alloc]init];
myTabBar.view.bounds=[UIScreen mainScreen].bounds;
UIWindow *window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:myTabBar];
window.rootViewController = nav;
self.window = window;
[window makeKeyAndVisible];
return YES;
}
然后在myTabBarController.m中
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self addChildViewController:childVCMain(@"首頁", @"user_00084", 0)];
[self addChildViewController:childVCTool(@"工具", @"Tools_00028", 1)];
[self addChildViewController:childVCMine(@"我的", @"drop", 2)];
self.tabBar.tintColor = [UIColor colorWithRed:234/255.0 green:111/255.0 blue:90/255.0 alpha:1];
if (@available(iOS 10.0, *)) {
self.tabBar.unselectedItemTintColor = [UIColor colorWithRed:124/255.0 green:99/255.0 blue:86/255.0 alpha:1];
} else {
// Fallback on earlier versions
}
}
UIViewController *childVCMain(NSString *title,NSString *imageName,NSInteger tag){
mainViewController * vc = [[mainViewController alloc]init];
vc.view.backgroundColor = [UIColor redColor];
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imageName] tag:tag];
return vc;
}
UIViewController *childVCTool(NSString *title,NSString *imageName,NSInteger tag){
toolViewController * vc = [[toolViewController alloc]init];
vc.view.backgroundColor = [UIColor yellowColor];
vc.tabBarItem = [[UITabBarItem alloc]initWithTitle:title image:[UIImage imageNamed:imageName] tag:tag];
return vc;
}
UIViewController *childVCMine(NSString *title,NSString *imageName,NSInteger tag){
mineViewController * vc = [[mineViewController alloc]init];
vc.view.backgroundColor = [UIColor blueColor];
vc.navigationItem.title = @"個人中心";
vc.tabBarItem = [[UITabBarItem alloc]initWithTitle:title image:[UIImage imageNamed:imageName] tag:tag];
return vc;
}
結(jié)果這樣三個頁面同時使用一個UINavigationController兵志,造成不顯示頁面的navigationItem.title玛瘸。所以每個頁面都各自分配一個UINavigationController奥裸,問題解決值纱。
//刪掉AppDelegate中的
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:myTabBar];
在每個自定義childVC中增加
UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:vc];
return nvc;
問題解決搀捷。窿冯。。還是對結(jié)構(gòu)不熟練才會出這樣的問題