如何以编程方式设置导航栏的标题?

发布于 2024-11-10 06:35:36 字数 52 浏览 0 评论 0原文

我的视图中有一个导航栏,我想以编程方式更改其导航项标题。

这是怎么做到的?

I have a Navigation Bar in a view, and I want to change its Navigation Item title programmatically.

How is this done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

倾城花音 2024-11-17 06:35:37

你也可以使用

You can also use ????????

[self.navigationController.navigationBar.topItem setTitle:@"Titlet"];
箜明 2024-11-17 06:35:37

这段代码对我不起作用

self.navigationItem.title = @"Title here";

但这有效。

self.title = "Title Name"

This code did not work for me

self.navigationItem.title = @"Title here";

But this worked.

self.title = "Title Name"
高速公鹿 2024-11-17 06:35:37

在 ViewDidLoad 方法中使用它

格式:

@property(nonatomic,readonly,retain) UINavigationItem *navigationItem; // Created on-demand so that a view controller may customize its navigation appearance.

示例:

self.navigationItem.title = @"Title here";

Use it in ViewDidLoad method

Format :

@property(nonatomic,readonly,retain) UINavigationItem *navigationItem; // Created on-demand so that a view controller may customize its navigation appearance.

Example :

self.navigationItem.title = @"Title here";
别忘他 2024-11-17 06:35:37

非常重要的注意事项:确保在父视图控制器的 viewDidLoad 方法或情节提要中的父导航栏中进行上述更改

very important note : make sure you make the changes that mentioned above in parent view Controller's viewDidLoad method, or parents navigation bar in storyboard

留蓝 2024-11-17 06:35:37

有时您可能会遇到这样的情况:您正在更新标题,但它没有造成任何效果,这可能是当您在 topItem 上有自定义视图时,因此您可能需要像这样更新它:< code>(self.navigationController?.navigationBar.topItem?.titleView as? UILabel)?.text = "我快乐的新标题"

Sometimes you might get a case where you are updating the title and it is not causing any effect, this can be when you have a custom view on the topItem therefore you might want to update it like this: (self.navigationController?.navigationBar.topItem?.titleView as? UILabel)?.text = "My Happy New Title"

审判长 2024-11-17 06:35:37

Swift:

self.navigationItem.title = "The title;

目标 c

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

SwiftUI

.navigationBarTitle("Your Title Here")

Swift:

self.navigationItem.title = "The title;

Objective c

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

SwiftUI

.navigationBarTitle("Your Title Here")
记忆之渊 2024-11-17 06:35:36

在你的 UIViewController 中

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

In your UIViewController

self.navigationItem.title = @"The title";
森林迷了鹿 2024-11-17 06:35:36

Swift 3 版本:

如果您使用没有导航控制器的导航栏,那么您可以尝试以下操作:

navigationBar.topItem?.title = "Your Title"

希望得到帮助。

Swift 3 Version:

If you use a navigation bar without navigation controller, then you can try this:

navigationBar.topItem?.title = "Your Title"

Hope for help.

最好是你 2024-11-17 06:35:36

在 viewDidLoad 中

  self.title = @"Title";

In viewDidLoad

  self.title = @"Title";
养猫人 2024-11-17 06:35:36

viewDidLoad 中编写以下代码,或者在 initWithNibName: 下编写可能更好,

  self.navigationItem.title = @"title";

谢谢。

Write the below code in viewDidLoad or may be better would be under initWithNibName:

  self.navigationItem.title = @"title";

Thanks.

笑梦风尘 2024-11-17 06:35:36

来自 Apple 文档

使用导航栏最常见的方式是使用导航栏
控制器。您还可以将导航栏用作独立对象
在您的应用程序中。

因此,如果您有一个 UINavigationController,则需要执行所有操作来设置导航栏的标题(如之前所有答案中所述),

self.navigationItem.title = @"title";

但是如果您有一个在 UIViewController 对象内以编程方式创建的独立导航栏,则必须通过创建适当的 UINavigationItem 对象并将它们添加到导航栏对象堆栈来设置导航栏的初始外观即

  1. 创建导航栏 (UINavigationBar) 的实例
  2. 创建管理按钮和视图的导航栏项目 (UINavigationItem) 的实例显示在 UINavigationBar 对象中。请注意,您在此步骤设置了标题。
  3. (可选步骤)将右/左按钮(UIBarButtonItem 的实例)添加到导航栏项目。

上述步骤的 Objective-C 代码示例:

UINavigationBar* navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];

/* Create navigation item object & set the title of navigation bar. */
UINavigationItem* navItem = [[UINavigationItem alloc] initWithTitle:self.shoppingItem.name];

/* Create left button item. */
UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onTapCancel:)];
navItem.leftBarButtonItem = cancelBtn;

/* Create left button item. */
UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onTapDone:)];
navItem.rightBarButtonItem = doneBtn;

/* Assign the navigation item to the navigation bar.*/
[navbar setItems:@[navItem]];

/* add navigation bar to the root view.*/
[self.view addSubview:navbar];

对于独立导航栏的 Swift 版本,请查看 这个答案

From Apple Documentation:

The most common way to use a navigation bar is with a navigation
controller. You can also use a navigation bar as a standalone object
in your app.

So If you have a UINavigationController, all what you have to do to set the title of the navigation bar (as explained in all previous answers)

self.navigationItem.title = @"title";

But If you have a standalone navigation bar that is created programmatically within an object of UIViewController, You have to set the initial appearance of the navigation bar by creating the appropriate UINavigationItem objects and adding them to the navigation bar object stack i.e.

  1. Create an instance of navigation bar (UINavigationBar)
  2. Create an instance of navigation bar item (UINavigationItem) that manages the buttons and views to be displayed in a UINavigationBar object. Note that you set the title at this step.
  3. (Optional Step) Add right/left buttons (instances of UIBarButtonItem) to the navigation bar item.

Sample of Objective-C Code for the mentioned steps:

UINavigationBar* navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];

/* Create navigation item object & set the title of navigation bar. */
UINavigationItem* navItem = [[UINavigationItem alloc] initWithTitle:self.shoppingItem.name];

/* Create left button item. */
UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onTapCancel:)];
navItem.leftBarButtonItem = cancelBtn;

/* Create left button item. */
UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onTapDone:)];
navItem.rightBarButtonItem = doneBtn;

/* Assign the navigation item to the navigation bar.*/
[navbar setItems:@[navItem]];

/* add navigation bar to the root view.*/
[self.view addSubview:navbar];

For Swift version of the standalone navigation bar, check out this answer.

被翻牌 2024-11-17 06:35:36

在 UIViewController 的 viewDidLoad 中

self.navigationItem.title = "The title";  //In swift 4

,或者

您可以直接

self.title = "The title"

执行此操作,

您也可以将上述语句放在应用程序委托中以进行全局复制。

In your UIViewController's viewDidLoad

self.navigationItem.title = "The title";  //In swift 4

or

you can just

self.title = "The title"

will do the trick

you can also put the above statement in the app delegate to replicate globally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文