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.
-
1Are 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– Dai2017-01-17 06:59:52 +00:00Commented Jan 17, 2017 at 6:59
-
4Possible duplicate of Changing navigation title programmaticallyAhmad F– Ahmad F2017-01-17 07:05:24 +00:00Commented 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 ?Harry– Harry2017-01-17 08:10:51 +00:00Commented Jan 17, 2017 at 8:10
Add a comment
|
4 Answers
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Your title over here"
}
1 Comment
Emre Tufekci
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.
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
Swift 4
This will change the top item of the navigation bar's stack
navigationController?.navigationBar.topItem?.title = "TEST"
2 Comments
WikipediaBrown
This doesn't answer the question.
Emre Tufekci
@WikipediaBrown actually it is the best answer.
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
WikipediaBrown
This doesn't answer the question.