自定义导航栏不会更改/显示标题

时间:2014-07-07 08:11:56

标签: ios objective-c uinavigationbar

我在故事板中添加了UINavigationBar。我的应用程序必须动态更改栏的标题。首先,我尝试在故事板中更改标题并运行应用程序,标题没有出现。然后我尝试按代码添加标题。首先,我将IBOutlet中的UINavigationItem与我的@interface相关联,并尝试添加如下标题:

_navibarTitle.title = @"Something";

它也没有成功,标题也没有出现。由于我的应用必须隐藏UINavigationController的默认栏并使用自定义栏,我甚至尝试过:

self.navigationItem.title = @"Something";

和此:

self.navigationController.navigationItem.title = @"HI";
他们也没有成功。 (最后两个我知道他们不会工作,因为隐藏了默认的导航栏)。所以我想我没有想法。可能有什么不对?

2 个答案:

答案 0 :(得分:1)

为屏幕或导航栏设置标题的简单且最佳方法是:

self.title = @"Your Title"

它会自动在导航栏上显示您的标题。您也可以从this问题获得帮助。

其他方式:

    UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
    navLabel.backgroundColor = [UIColor clearColor];
    navLabel.textAlignment = NSTextAlignmentCenter;
    navLabel.text = @"Your Title";
    self.navigationItem.titleView = navLabel;

我希望这会奏效。

答案 1 :(得分:0)

试试这个:

self.navigationController.navigationBar.tintColor = [UIColor redColor];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.font = [UIFont systemFontOfSize:20];
self.navigationItem.titleView = titleLabel;
titleLabel.text = @"Something";
[titleLabel sizeToFit];