简单标题未显示在UINavigationController中
我已经查看了所有相似/相关的问题,但是没有a)正是我的问题或2)解决方案不起作用。
在我的appDelegate.m中,我有didFinishLaunchingWithOptions
JCGRootNavigationController *rnc = [[JCGRootNavigationController alloc] init]; self.window.rootViewController = rnc;`
JCGRootNavigationController是UINavigationController的子类
@interface JCGRootNavigationController : UINavigationController`
在JCGRootNavigationController.m中:
@implementation JCGRootNavigationController -(instancetype) init { self = [super init]; self.view.backgroundColor = [UIColor lightGrayColor]; self.navigationItem.title = @"MY TITLE"; return self; }
并且标题不会显示。 我看到导航栏,但没有标题。 看起来这些年来很多人都有同样的问题。 也许一个简单的答案将有助于澄清所有令人困惑的问题。 这太令人难以置信了。
UINavigationController自动显示它正在显示的UIViewController子类的标题。 它通过查看该UIViewController或UIViewController子类的navigationItem.title属性来实现。 基本上UINavigationController没有标题。
使用Storyboard时,设置UIViewController
或UITableViewController
的title
似乎并没有将该标题添加到导航控制器,正如其他答案所示。
而是在Storyboard面板中找到可能与View Controller对象一起的UINavigationItem
。 设置此导航项的title
以将该标题应用于导航控制器。
谢谢thinkInSanta,虽然我无法在任何地方找到苹果文档中明确说明的内容,但我必须将此作为答案。 UINavigationController没有标题。
为了让标题起作用,回到appDelegate.h我添加了:
JCGTableViewController *tvc = [[JCGTableViewController alloc] init]; JCGRootNavigationController *rnc = [[JCGRootNavigationController alloc] initWithRootViewController:tvc]; self.window.rootViewController = rnc;
其中JCGTableViewController是我添加的另一个子类。 您可以告诉它,它是UITableViewController的子类。
在JCGTableViewController中,我将init覆盖为:
-(instancetype) init { self = [super init]; if(self) { self.title = @"TVC"; self.view.backgroundColor = [UIColor lightGrayColor]; } return self; }
虽然我使用了tableViewController,但我想你可以将任何视图添加到NavigationController并以这种方式设置属性。 今天我会玩弄它。
谢谢大家!