ios 以编程方式更改导航栏标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10895122/
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
Changing Nav Bar title programmatically
提问by mhorgan
I'm trying to change my nav bar's title programmatically but the code I'm using won't work.
我正在尝试以编程方式更改导航栏的标题,但我使用的代码不起作用。
self.navigationController.navigationBar.tintColor = [UIColor redColor];
self.navigationController.navigationItem.title=@"Hello";
I can manage to change the colour of the bar but not the text.
我可以设法更改栏的颜色,但不能更改文本。
回答by Martol1ni
self.navigationController.navigationBar.topItem.title = @"YourTitle";
回答by Nim Thitipariwat
or self.navigationItem.title = @"Title";
或者 self.navigationItem.title = @"Title";
回答by Mudit Bajpai
First check the index of current controller on the navigation stack through
首先通过查看当前控制器在导航堆栈上的索引
NSLog(@"%@", self.navigationController.viewControllers );
after finding the index of current ViewController let say 1.
找到当前 ViewController 的索引后,假设为 1。
UINavigationController *navCon = (UINavigationController*) [self.navigationController.viewControllers objectAtIndex:1];
navCon.navigationItem.title = @"Hello";
回答by Sameera R.
You can use built in method setTitlenow in XCode[self setTitle:@"title"]
or self.title=@"title"
inside its viewController.
您现在可以在 XCode或其viewController 中使用内置方法setTitle。[self setTitle:@"title"]
self.title=@"title"
回答by apinho
For swift 2.2
对于SWIFT 2.2
navigationController?.navigationBar.topItem?.title = "Superman title"
回答by jlink
Try with this :
试试这个:
[navigationController setTitle:@"Title"];