14
votes

I am developing an app in Swift 2.2. Now I want to change the back button font and color for a certain view. The view in question has a navigation controller as it's parent controller.

I've tried running both of the following lines in viewDidLoad of my ViewController

self.navigationController!.navigationItem.backBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal)
self.navigationItem.backBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal)

Neither throws any errors, but it doesn't make any difference to the back button. I've also tried running both of these

self.navigationController!.navigationItem.leftBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal)   
self.navigationItem.leftBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal)

This however throws an error (error unwrapping nil). How should I change the font and color of the nav bar back button correctly? Feels like I'm not modifying the right items...

8
Set tintColor of your navigationBar in viewWillAppear: method and set it back in viewWillDisappear: methodTheTiger
use code in appdelegate didFinishLaunchingWithOptions if you want effect in whole appEI Captain v2.0

8 Answers

19
votes

If you want to set same color to bar buttons implicitly then in your appdelegate in didfinishlaunchingwithoption write,

 UINavigationBar.appearance().tintColor = UIColor.whiteColor() //your desired color here

Update :

put this in appdelegae,

 UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Andes Rounded", size: 17)!], forState: .Normal) // your textattributes here

Update 2 :

  UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UINavigationBar.classForCoder()]).setTitleTextAttributes(["attribute" : "value"], forState: .Normal)

Hope this will help :)

15
votes

Swift 3.0 answer (based on Lion's answer):

let newFont = UIFont(name: "Avenir Next", size: 16.0)!
let color = UIColor.white

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.classForCoder() as! UIAppearanceContainer.Type]).setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: newFont], for: .normal)

Works a treat, for those that have already managed to customise other parts of their nav bars but not the back button!

5
votes

I think you should change it in the vc before your actual vc. Look at: UINavigationItem

Edit: For example you can write:

let item = UIBarButtonItem(title: "Text goes here", style: .Plain, target: self, action: #selector(self.navigationController?.popViewControllerAnimated(_:)))

item.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Helvetica-Bold", size: 23)!], forState: .Normal)

navigationItem.backBarButtonItem = item

in your prepareForSegue Method.

5
votes

Swift 4

in AppDelegate.swift

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)
4
votes

in swift 4.2

to change back button color
self.navigationController?.navigationBar.tintColor = .white
3
votes

create custom button and make it as you want and add action to go back.

func addBackBarButtonOnNavigationBar(){
   // add image here
    let searchImage:UIImage = UIImage(named: "back button image")!

     var backBtn:UIBarButtonItem = UIBarButtonItem(image: searchImage,  style: UIBarButtonItemStyle.Plain, target: self, action: #selector(classname.buttonActionMethodName(_:)))
    backBtn.tintColor = UIColor.lightGrayColor()
  if let font = UIFont(name: "AvenirNext", size: 15) {
    backBtn.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}
    self.navigationItem.leftBarButtonItem = backBtn

}

func buttonActionMethodName(){
  self.navigationController!.popViewControllerAnimated(true)

}
2
votes

Use the following code:

    navigationController?.navigationBar.barTintColor = UIColor.purpleColor()
    navigationController?.navigationBar.tintColor = UIColor.whiteColor()

change colour according to your need

0
votes

In Swift 5 you can do it by these:

    self.navigationItem.backBarButtonItem?.tintColor = UIColor.red
    let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .regular)]
    self.navigationItem.backBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)

Please note it will be effective for the next pushed view controller not the current one on the display, that's why it's very confusing!

Also, check the storyboard and select the navigation item of the previous view controller then type something in the Back Button (Inspector).