好贴--iOS-关于UINavigationController的title不显示问题

用继承自UINavigationController与UITabbarController 创建类,将前者设置为RootViewController,出现了一个问题,navigetionController的title不显示,在controller里面怎么设置都不显示,原因是这个样子的:

title这个属性实际上是属于UIViewController而不属于UINavigationController。所以,这个属性是从UIViewController上面继承过来的。而不是UINavigationController上面的名字。由于UINavigationController属于容器,所以最少需要一个RootController。然后在RootController的viewDidLoad设置title而不是在UINavigationController的subclass中设置。而且viewDidLoad设置的title是统一显示的,导航视图控制的UIViewController的title都是一样的,我的解决方法是:将逻辑更换,UITabbarController设置为RootViewController,

 1 -(void)addTage:(Class)controller addName:(NSString *)name addImage:(NSString *)image addSelectImage:(NSString *)selectImage
 2 
 3 {
 4 
 5     
 6 
 7     UIViewController *vc = [[controller alloc] init];
 8 
 9     UINavigationController *cityNav = [[UINavigationControlleralloc]initWithRootViewController:vc];
10 
11     cityNav.tabBarItem.image = [UIImage imageNamed:image];
12 
13     cityNav.tabBarItem.selectedImage = [UIImage imageNamed:selectImage];
14 
15     cityNav.navigationBar.backgroundColor = [UIColor blueColor];
16 
17     cityNav.tabBarItem.title = name;
18 
19     cityNav.navigationBar.barStyle = UIBarStyleDefault;
20 
21     cityNav.navigationBar.barTintColor = [UIColor cyanColor];
22 
23     [self.navArray addObject:cityNav];
24 
25     
26 
27 }
28 
29  
30 
31     self.tabBarController.viewControllers = self.navArray;
32 
33  
34     self.window.rootViewController = self.tabBarController;

    self.navigationItem.title

    = @"my title"; sets navigation bar title.

    

    self.tabBarItem.title

    = @"my title"; sets tab bar title.

    

    self.title

    = @"my title"; sets both of these.

 

posted @ 2017-10-29 21:19  vector11248  阅读(3418)  评论(0编辑  收藏  举报