ios 设置导航栏标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41764349/
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
Set title of navigation bar
提问by Uffo
So I have a basic view controller with a navigation bar, this is view controller B, so I'm performing a segue to go here, and I'm trying to change the title like this:
所以我有一个带有导航栏的基本视图控制器,这是视图控制器 B,所以我正在执行一个转场到这里,我正在尝试像这样更改标题:
override func viewDidLoad() {
debugPrint(self.selectedName)
super.viewDidLoad();
self.navigationItem.title = "A NEW TITLE"
}
But it doesn't do anything
但它什么也不做
回答by Mamta
For Swift 3:
对于 Swift 3:
If you want to set just the navigation title without using a UINavigationController
then make an outlet of the navigation item as
如果您只想设置导航标题而不使用 aUINavigationController
则将导航项的出口设置为
@IBOutlet weak var navItem: UINavigationItem!
and then in viewDidLoad()
write
然后在viewDidLoad()
写
navItem.title = "ANY TITLE"
回答by Ganesh Manickam
To set Title on NavigationBar simply we can do by below code
要在 NavigationBar 上简单地设置标题,我们可以通过以下代码进行
self.title = "Your Title"
回答by onCompletion
There are plenty of methods which you can follow to achieve this. Here is few of those.
有很多方法可以用来实现这一目标。这里有几个。
First things first.
先说第一件事。
If you are ready to "Embed in" a navigation controller or if you have already one then you can access that using following code.
如果您已准备好“嵌入”导航控制器,或者您已经有一个导航控制器,那么您可以使用以下代码访问它。
self.navigationController?.navigationBar.topItem?.title = "Your Title"
Now for more customization:
现在进行更多定制:
- Place a
UINavigationBar
object on ViewController scene and add constraints to it. Make an outlet of newly placed
UINavigationBar
like as follows -@IBOutlet weak var orderStatusNavigationbar: UINavigationBar!
Now set the title using the outlet.
orderStatusNavigationbar.topItem?.title = "Your Title"
UINavigationBar
在 ViewController 场景上放置一个对象并为其添加约束。制作一个新放置的插座,
UINavigationBar
如下所示 -@IBOutlet weak var orderStatusNavigationbar: UINavigationBar!
现在使用插座设置标题。
orderStatusNavigationbar.topItem?.title = "Your Title"
All this above code are in Swift 3 but will work on lower versions of Swift also(atleast on Swift 2.0)
以上所有代码都在 Swift 3 中,但也适用于较低版本的 Swift(至少在 Swift 2.0 上)
Hope this helped.
希望这有帮助。
回答by rust
Swift 4 / XCode 10
斯威夫特 4 / XCode 10
Add new NavigationBar
->
Drap and Drop it to your viewPress CTRLto add new Outlet Action
Example :
@IBOutlet weak var main_navbar: UINavigationBar
inViewController
class.Then set the title :
main_navbar.topItem?.title = "YOUR TITLE"
添加新的 NavigationBar
->
Drap 并将其拖放到您的视图中按下CTRL以添加新的插座操作
例子:
@IBOutlet weak var main_navbar: UINavigationBar
在ViewController
课堂上。然后设置标题:
main_navbar.topItem?.title = "YOUR TITLE"
This solution worked for me, hope it does for you too.
- rust
回答by Saheb Roy
Ok, So you need to embed the whole thing in a Navigation Controller first, and then make that navigation controller as the initial controller.
好的,所以您需要先将整个内容嵌入到导航控制器中,然后将该导航控制器作为初始控制器。
Select your storyboard, click the first controller then click this -
选择你的故事板,点击第一个控制器,然后点击这个 -
Then you remove the navigation bar you set(put) over the last controller named "title".
然后删除您在最后一个名为“title”的控制器上设置(放置)的导航栏。
The reason this didnt work, as you are trying to change the title of the navigation controller's navigation bar, but it doesnt have it, hence it cant change it.
这不起作用的原因,因为您正在尝试更改导航控制器导航栏的标题,但它没有它,因此它无法更改它。