想要设置一个同时使用UINavigationController和UITabBarController的结构,我在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;
}
结果这样三个页面同时使用一个UINavigationController,造成不显示页面的navigationItem.title。所以每个页面都各自分配一个UINavigationController,问题解决。
//删掉AppDelegate中的
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:myTabBar];
在每个自定义childVC中增加
UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:vc];
return nvc;
问题解决。。。还是对结构不熟练才会出这样的问题
网友评论