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

本文探讨了在iOS开发中使用UINavigationController时遇到的title不显示的问题,并提供了解决方案。作者建议不要直接在UINavigationController的子类中设置title,而是应该在作为根视图控制器的UIViewController中设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用继承自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.

 

转载于:https://www.cnblogs.com/vector11248/p/7751305.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值