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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 11:04:51  来源:igfitidea点击:

Set title of navigation bar

iosswiftuinavigationbar

提问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

但它什么也不做

enter image description here

在此处输入图片说明

回答by Mamta

For Swift 3:

对于 Swift 3:

If you want to set just the navigation title without using a UINavigationControllerthen 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:

现在进行更多定制:

  1. Place a UINavigationBarobject on ViewController scene and add constraints to it.
  2. Make an outlet of newly placed UINavigationBarlike as follows -

    @IBOutlet weak var orderStatusNavigationbar: UINavigationBar!
    
  3. Now set the title using the outlet.

    orderStatusNavigationbar.topItem?.title = "Your Title"
    
  1. UINavigationBar在 ViewController 场景上放置一个对象并为其添加约束。
  2. 制作一个新放置的插座,UINavigationBar如下所示 -

    @IBOutlet weak var orderStatusNavigationbar: UINavigationBar!
    
  3. 现在使用插座设置标题。

    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

  1. Add new NavigationBar ->Drap and Drop it to your view

  2. Press CTRLto add new Outlet Action

  3. Example : @IBOutlet weak var main_navbar: UINavigationBarin ViewControllerclass.

  4. Then set the title : main_navbar.topItem?.title = "YOUR TITLE"

  1. 添加新的 NavigationBar ->Drap 并将其拖放到您的视图中

  2. 按下CTRL以添加新的插座操作

  3. 例子:@IBOutlet weak var main_navbar: UINavigationBarViewController课堂上。

  4. 然后设置标题: main_navbar.topItem?.title = "YOUR TITLE"

This solution worked for me, hope it does for you too. - rustenter image description here

这个解决方案对我有用,希望它也适合你。- 生锈在此处输入图片说明

回答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 -

选择你的故事板,点击第一个控制器,然后点击这个 -

img

图片

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.

这不起作用的原因,因为您正在尝试更改导航控制器导航栏的标题,但它没有它,因此它无法更改它。