ios UINavigationBar - 以编程方式设置标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3680805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:42:18  来源:igfitidea点击:

UINavigationBar - Set title programmatically?

iosiphonexcode4uinavigationbar

提问by AveragePro

I have a tab bar application with a different view on each tab. Each view has a UINavigationBar with title set on Interface Builder. I want to change the title based on a clause in the ViewDidLoad method, so if x { change the title }.

我有一个标签栏应用程序,每个标签上都有不同的视图。每个视图都有一个 UINavigationBar,其标题设置在 Interface Builder 上。我想根据 ViewDidLoad 方法中的一个子句更改标题,因此如果 x { 更改标题 }。

I have tried self.title = @"title", but this changes the title of the tab bar item itself.

我试过self.title = @"title",但这会改变标签栏项目本身的标题。

So, how is this done?

那么,这是如何做到的呢?

回答by Greg

I've set the title programmatically using code something like this:

我已经使用如下代码以编程方式设置标题:

navBar.topItem.title = @"title";

where navBar is declared as an IBOutlet UINavigationBar linked to the navigation bar in interface builder. This worked in my app; however, I was not using a tab bar.

其中 navBar 被声明为一个 IBOutlet UINavigationBar 链接到界面构建器中的导航栏。这在我的应用程序中有效;但是,我没有使用标签栏。

If navBar.topItem is the tab bar item, I don't see a way for you to change the title that appears on the navigation bar without also changing the title on the tab bar item, since the navBar's topItem and the tab bar item is the same object.

如果 navBar.topItem 是标签栏项目,我看不到您可以更改导航栏上显示的标题而不更改标签栏项目上的标题的方法,因为导航栏的 topItem 和标签栏项目是同一个对象。

回答by ChrisP

Use

self.navigationItem.title = @"the title";

as setting navBar.topItem.titlewill not work in all circumstances.

因为设置navBar.topItem.title不会在所有情况下都有效。

回答by Muzammil

Use this :

用这个 :

    CGRect navBarFrame = CGRectMake(0, 0, self.tableView.frame.size.width, 44.0);
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:navBarFrame];

    UINavigationItem *navItem = [UINavigationItem alloc];
    navItem.title = @"Your Title";

    [navBar pushNavigationItem:navItem animated:false];

[self.view addSubView:navBar]; 

or

或者

self.tableView.tableHeaderView = navBar; etc

回答by Augustine P A

If the class is a type of UIViewController, then you can set title as given in the viewDidLoad method.

如果类是 UIViewController 的类型,那么您可以设置 viewDidLoad 方法中给出的标题。

    [self setTitle:@"My Title"];

回答by iAnkit

In ViewDidLoad of your ViewController.m just write,

在 ViewController.m 的 ViewDidLoad 中,只需写下,

self.navigationItem.title=@"Hello World";

Here is the Output:

这是输出:

enter image description here

在此处输入图片说明

回答by Shyam

Create IBOutletof UINavigationBar

创建IBOutletUINavigationBar

navigationBar.topItem.title = @"Title";

Hope this helps.

希望这可以帮助。

回答by Tod Cunningham

I used the following:

我使用了以下内容:

self.navigationController.navigationBar.topItem.title = @"Title";

However, I also had to call it in a dispatch block as it wouldn't change the title when called within the viewDidLoad w/o it.

但是,我还必须在调度块中调用它,因为在没有它的 viewDidLoad 中调用时它不会更改标题。

回答by Mike

Note that in order for the title to show up AT ALL the Navigation Controller's delegate MUST be set to the navigation bar itself or the title will NEVER show!

请注意,为了让标题完全显示,导航控制器的委托必须设置为导航栏本身,否则标题将永远不会显示!

回答by donmiller

You can also just set the title in the ViewDidLoad under your ViewController or TableViewController using

您也可以使用在 ViewController 或 TableViewController 下的 ViewDidLoad 中设置标题

_title = @"Title";

or

或者

self.title = @"Title";

回答by Kiran eldho

Try this,

尝试这个,

self.navigationItem.title = @"Your title";