Set title of navigation bar
Swift 4 / XCode 10
Add new NavigationBar
->
Drap and Drop it to your viewPress CTRL to add new Outlet Action
Example :
@IBOutlet weak var main_navbar: UINavigationBar
inViewController
class.Then set the title :
main_navbar.topItem?.title = "YOUR TITLE"
This solution worked for me, hope it does for you too. - rust
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"
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"