简体   繁体   English

自定义导航栏不变/显示标题

[英]Custom navigation bar doesn't change / show title

I added a UINavigationBar in storyboard. 我在情节UINavigationBar中添加了UINavigationBar My app has to change the title of the bar dynamically. 我的应用程序必须动态更改栏的标题。 First I tried changing the title in storyboard and ran the app, the title didn't appear. 首先,我尝试在情节提要中更改标题并运行该应用程序,但标题没有出现。 I then tried adding the title by code. 然后,我尝试通过代码添加标题。 First, I connected an IBOutlet from the UINavigationItem to my @interface and tried adding a title like this: 首先,我将IBOutletUINavigationItem连接到我的@interface并尝试添加如下标题:

_navibarTitle.title = @"Something";

It also didn't work out, the title doesn't appear. 它也没有解决,标题没有出现。 Since my app has to hide the default bar of the UINavigationController and use custom ones, I even tried this: 由于我的应用程序必须隐藏UINavigationController的默认栏并使用自定义栏,因此我什至尝试了以下操作:

self.navigationItem.title = @"Something";

and this: 和这个:

self.navigationController.navigationItem.title = @"HI";

they also didn't work out. 他们也没有解决。 (the last two i know they wouldn't work as the default navi bar is hidden). (我知道最后两个将不起作用,因为默认的导航栏被隐藏了)。 So i guess i'm out of ideas. 所以我想我没主意了。 What could be wrong? 有什么事吗

The simple and best way to set title for a screen or navigation bar is: 为屏幕或导航栏设置标题的简单最佳方法是:

self.title = @"Your Title"

It will show your title on navigation bar automatically. 它将自动在导航栏上显示您的标题。 You can get help from this question as well. 您也可以从问题获得帮助。

Other way to do same: 其他执行相同操作的方法:

    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;

I hope this will work. 我希望这会起作用。

try this: 尝试这个:

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM