UINavigationController子类不会在加载时设置titleView [英] UINavigationController subclass won't set titleView on load

查看:74
本文介绍了UINavigationController子类不会在加载时设置titleView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建了 UINavigationController 的简单子类。

I've just created a simple subclass of UINavigationController.

#import "TTNavigationController.h"

@implementation TTNavigationController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *logo = [UIImage imageNamed:@"logo"];
    UIImageView *logoView = [[UIImageView alloc] initWithImage:logo];
    self.navigationItem.titleView = logoView;
}

但是当我运行项目时,导航栏没有 titleView 和徽标。

But when I run the project the navigation bar has not the titleView with the logo.

否则,如果我尝试将这3行代码移到 viewDidLoad 我的视图控制器工作正常。为什么?

Otherwise if I try to move those 3 lines of code in viewDidLoad of my view controller works fine. Why?

推荐答案

您不能使用 self.navigationItem ,因为从未使用过导航控制器拥有的 navigationItem 。它具有此属性是因为它是一种视图控制器,但是由于 viewControllers (或更具体地说,是 topViewController s) navigationItem 显示。

You can't use self.navigationItem because the navigation controllers own navigationItem is never used. It has this property because it is a type of view controller but it isn't actually displayed because the viewControllers (or, more specifically, the topViewControllers) navigationItem is displayed.

因此,更改视图控制器 navigationItem (直接或在按下时)。

So, change the view controller navigationItem (either directly, or when it is pushed).

这篇关于UINavigationController子类不会在加载时设置titleView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆