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.

3
  • 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? Commented Jan 17, 2017 at 6:59
  • 4
    Possible duplicate of Changing navigation title programmatically Commented Jan 17, 2017 at 7:05
  • @Dai: I am using Storyboard and I have a code outlet for the navigation bar. What should I do ? Commented Jan 17, 2017 at 8:10

4 Answers 4

7
override func viewDidLoad() {
     super.viewDidLoad()
     self.title = "Your title over here"
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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)!]  

Comments

3

Swift 4

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

navigationController?.navigationBar.topItem?.title = "TEST"

2 Comments

This doesn't answer the question.
@WikipediaBrown actually it is the best answer.
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.

1 Comment

This doesn't answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.