3

I am not using navigation controller but a navigation bar. But I couldn't find any solution to change the title programmatically. Please provide a solution in swift.

Harry
  • 137
  • 1
  • 2
  • 5
  • 1
    Are you using a Storyboard, XIB, or manually building the UI in code? Do you have a code outlet for the navigation bar? If not, why not? – Dai Jan 17 '17 at 06:59
  • 4
    Possible duplicate of [Changing navigation title programmatically](http://stackoverflow.com/questions/25167458/changing-navigation-title-programmatically) – Ahmad F Jan 17 '17 at 07:05
  • @Dai: I am using Storyboard and I have a code outlet for the navigation bar. What should I do ? – Harry Jan 17 '17 at 08:10

4 Answers4

6
override func viewDidLoad() {
     super.viewDidLoad()
     self.title = "Your title over here"
}
Dory
  • 7,462
  • 7
  • 33
  • 55
  • 1
    This is not good way to do this. If you would like to use, bottom navigation bar and top navigation at the same time with this solution both of them will be affected. – Emre Tufekci Jan 10 '22 at 13:09
6

Try below code inside your viewDidLoad.

// To Set your navigationBar title.
yourNavBarName.topItem?.title = "Some Title"

// To Set your navigationBar backgound.
yourNavBarName.barTintColor = .red 

// To Set your navigationBar title font and color.
yourNavBarName.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.green, NSFontAttributeName:UIFont(name:"HelveticaNeue", size: 26)!]  
Phyber
  • 1,368
  • 11
  • 25
Joe
  • 8,868
  • 8
  • 37
  • 59
3

Swift 4

This will change the top item of the navigation bar's stack

navigationController?.navigationBar.topItem?.title = "TEST"
suisied
  • 426
  • 1
  • 6
  • 19
0

To change the title in a specific View controller use this:

navigationItem.setCustomTitle("Title", font: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium), textColor: .white)

This way you don't have to change it back if you pushed from another navigation controller that has a title.

ricks
  • 3,154
  • 31
  • 51