【问题标题】:Simple Title not showing up in UINavigationControllerUINavigationController 中未显示简单标题
【发布时间】:2014-07-03 22:24:24
【问题描述】:

我查看了所有类似/相关的问题,但没有一个 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;
}

而且标题不会显示。我看到导航栏,但没有标题。看起来多年来很多人都遇到过同样的问题。也许一个简单的答案将有助于消除所有令人困惑的问题。这真是令人难以置信的令人沮丧。

【问题讨论】:

  • 您需要设置导航控制器正在显示的视图控制器的标题属性。只有将 JCGRootNavigationController 放在另一个 UINavigationController 中时,您设置的标题才会显示。
  • 你试过self.title吗?
  • 我确实尝试了self.title,但也没有用。

标签: ios uinavigationcontroller uinavigationitem uitabbaritem


【解决方案1】:

使用 Storyboard 时,设置 UIViewControllerUITableViewControllertitle 似乎不会将该标题添加到导航控制器,正如其他答案所暗示的那样。

相反,在 Storyboard 层次结构中找到可能与您的 View Controller 对象并排的UINavigationItem。设置此导航项的 title 以将该标题应用于导航控制器。

【讨论】:

  • 哦。我的。 $DEITY。 谢谢。我没想到 iOS 会忽略我的视图控制器的标题。
【解决方案2】:

UINavigationController 自动显示它正在显示的 UIViewController 子类的标题。它通过查看 UIViewController 或 UIViewController 子类的 navigationItem.title 属性来实现。基本上 UINavigationController 没有标题。

【讨论】:

    【解决方案3】:

    感谢 believeInSanta,虽然我无法在任何地方的苹果文档中找到明确说明,但我必须接受这个作为答案。 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 并以这种方式设置属性。我今天会玩这个。

    谢谢大家!

    【讨论】:

      猜你喜欢
      • 2011-01-03
      • 2017-10-28
      • 2011-05-13
      • 2012-04-29
      • 2014-12-03
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多