Set title of navigation bar

Swift 4 / XCode 10

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

  2. Press CTRL to add new Outlet Action

  3. Example : @IBOutlet weak var main_navbar: UINavigationBar in ViewController class.

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

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


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 UINavigationBar object on ViewController scene and add constraints to it.
  2. Make an outlet of newly placed UINavigationBar like as follows -

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

    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)

Hope this helped.


For Swift 3:

If you want to set just the navigation title without using a UINavigationController then make an outlet of the navigation item as

 @IBOutlet weak var navItem: UINavigationItem!

and then in viewDidLoad() write

navItem.title = "ANY TITLE"

To set Title on NavigationBar simply we can do by below code

self.title = "Your Title"