Ios 如何以编程方式设置导航栏的标题?
我在视图中有一个导航栏,我想以编程方式更改其导航项标题Ios 如何以编程方式设置导航栏的标题?,ios,iphone,view,uinavigationbar,Ios,Iphone,View,Uinavigationbar,我在视图中有一个导航栏,我想以编程方式更改其导航项标题 这是如何做到的?在您的UIViewController中 self.navigationItem.title = @"The title"; 在viewDidLoad中编写以下代码,或者最好在initWithNibName: self.navigationItem.title = @"title"; 谢谢。在viewDidLoad中 self.title = @"Title"; self.navigationItem.titl
这是如何做到的?在您的UIViewController中
self.navigationItem.title = @"The title";
在
viewDidLoad
中编写以下代码,或者最好在initWithNibName:
self.navigationItem.title = @"title";
谢谢。在viewDidLoad中
self.title = @"Title";
self.navigationItem.title = "The title"; //In swift 4
在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";
示例:
@property(nonatomic,readonly,retain) UINavigationItem *navigationItem; // Created on-demand so that a view controller may customize its navigation appearance.
self.navigationItem.title = @"Title here";
发件人:
使用导航栏最常用的方法是使用导航
控制器。也可以将导航栏用作独立对象
在你的应用程序中
因此,如果您有一个UINavigationController,那么您需要做的就是设置导航栏的标题(如前面所有答案中所述)
但是,如果在UIViewController的对象中有一个以编程方式创建的独立导航栏,则必须通过创建适当的UINavigationItem对象并将其添加到导航栏对象堆栈中来设置导航栏的初始外观,即
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版本,请查看。非常重要的注意:请确保在父视图控制器的viewDidLoad方法中进行了上述更改,或者故事板中的家长导航栏您也可以使用有时您可能会遇到这样的情况:您正在更新标题,但它不会产生任何影响,这可能是因为您在
topItem上有一个自定义视图
,因此您可能希望像这样更新它:(self.navigationController?.navigationBar.topItem?.titleView as?UILabel)?.text=“我的快乐新标题”
Swift 3版本:
如果使用不带导航控制器的导航栏,则可以尝试以下操作:
navigationBar.topItem?.title = "Your Title"
希望得到帮助。此代码对我无效
self.navigationItem.title = @"Title here";
但这起作用了
self.title = "Title Name"
在UIViewController的viewDidLoad中
self.title = @"Title";
self.navigationItem.title = "The title"; //In swift 4
或
你可以
self.title = "The title"
我会成功的
您还可以将上述语句放在应用程序委托中进行全局复制。My two Second:)这是可行的,尽管如果vewcontroller是toolbarcontroller的一部分。然后工具栏项的标题也会更新。是的,我会选择它,因为它只更新navigationItem的标题,而不更新工具栏项的标题。