16

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...

3
  • Set tintColor of your navigationBar in viewWillAppear: method and set it back in viewWillDisappear: method
    – TheTiger
    May 19, 2016 at 9:52
  • use code in appdelegate didFinishLaunchingWithOptions if you want effect in whole app May 19, 2016 at 10:08
  • The main nuance (not intuitive, tbh) here is that backBarButtonItem of current VC is visible only when you push another VC. That's why you didn't see any effect on the current VC.
    – Legonaftik
    Jun 5, 2022 at 16:02

8 Answers 8

20

If you want to set same color to bar buttons implicitly then in your AppDelegate, in didfinishlaunchingwithoptions, write:

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

Update :

Put this in AppDelegate,

 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 :)

2
  • Yeah this seems to work fine, thanks! I want to set the font as well globally, but it only seems to affect the title font, not the back button font... Any idea? May 19, 2016 at 10:15
  • Thats what I've done but it only affects the navbar title, not the back button :( May 19, 2016 at 10:25
15

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

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.

1
  • Can you explain your proposed solution more clearly and possibly in more details?
    – werediver
    May 19, 2016 at 13:06
5

Swift 4

in AppDelegate.swift

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

in swift 4.2

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

Use the following code:

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

change colour according to your need

3
  • I've tried that but it doesn't make any difference :( May 19, 2016 at 9:39
  • This works in my master controller but not in my detail controller. How could that be? May 19, 2016 at 9:56
  • use this code in AppDelegate.m file in didFinishLaunchingWithOptions it works for you surely
    – user5938635
    May 19, 2016 at 10:10
2

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)

}
1
  • This code will show the icon and will allow to perform an arbitrary action when you tap it. The problem is that setting leftBarButtonItem removes the default backBarButtonItem (not only the system icon but also the interactive dismissal gesture). A custom leftBarButtonItem will also have tappable area which matches the item size by default while the system backBarButtonItem has larger tap area so it's easier to hit it.
    – Legonaftik
    Jun 5, 2022 at 15:59
2

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.