ios 如何以编程方式设置导航栏的标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6154237/
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
How to set the title of a Navigation Bar programmatically?
提问by Doug Null
I have a Navigation Bar in a view, and I want to change its Navigation Item title programmatically.
我在视图中有一个导航栏,我想以编程方式更改其导航项标题。
How is this done?
这是怎么做的?
回答by arclight
In your UIViewController
在你的 UIViewController
self.navigationItem.title = @"The title";
回答by Jerome
Swift 3 Version:
斯威夫特 3 版本:
If you use a navigation bar without navigation controller, then you can try this:
如果你使用没有导航控制器的导航栏,那么你可以试试这个:
navigationBar.topItem?.title = "Your Title"
Hope for help.
希望得到帮助。
回答by Madhu
In viewDidLoad
在视图中加载
self.title = @"Title";
回答by Wael Showair
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)
所以如果你有一个 UINavigationController,你需要做的就是设置导航栏的标题(如之前所有答案中所述)
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.
但是如果你有一个在 UIViewController 对象中以编程方式创建的独立导航栏,你必须通过创建适当的 UINavigationItem 对象并将它们添加到导航栏对象堆栈来设置导航栏的初始外观,即
- Create an instance of navigation bar (UINavigationBar)
- 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.
- (Optional Step) Add right/left buttons (instances of UIBarButtonItem) to the navigation bar item.
- 创建导航栏实例(UINavigationBar)
- 创建一个导航栏项 (UINavigationItem) 的实例,用于管理要在 UINavigationBar 对象中显示的按钮和视图。请注意,您在此步骤中设置了标题。
- (可选步骤)向导航栏项添加左右按钮(UIBarButtonItem 的实例)。
Sample of Objective-C Code for the mentioned steps:
上述步骤的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];
For Swift version of the standalone navigation bar, check out this answer.
对于 Swift 版本的独立导航栏,请查看此答案。
回答by Surjeet Rajput
You can also use
你也可以使用
[self.navigationController.navigationBar.topItem setTitle:@"Titlet"];
回答by Nikhil Bansal
Write the below code in viewDidLoad
or may be better would be under initWithNibName:
写下下面的代码viewDidLoad
或者可能更好initWithNibName:
self.navigationItem.title = @"title";
Thanks.
谢谢。
回答by Prasad A
This code did not work for me
这段代码对我不起作用
self.navigationItem.title = @"Title here";
But this worked.
但这奏效了。
self.title = "Title Name"
回答by NickCoder
In your UIViewController's viewDidLoad
在你的 UIViewController 的 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.
您也可以将上述语句放在应用程序委托中以进行全局复制。
回答by Jayprakash Dubey
Use it in ViewDidLoad method
在 ViewDidLoad 方法中使用它
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";
回答by Hola Soy Edu Feliz Navidad
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"
有时您可能会遇到这样的情况,即您正在更新标题但它没有造成任何影响,这可能是当您有自定义视图时,topItem
因此您可能希望像这样更新它:(self.navigationController?.navigationBar.topItem?.titleView as? UILabel)?.text = "My Happy New Title"