Quick font change and back button color

I am developing an application in Swift 2.2. Now I want to change the font and color of the feedback button for a specific view. In this view, there is a navigation controller as the parent controller.

I tried to run both of the following lines in the 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) 

It does not cause errors, but it does not matter for the back button. I 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) 

However, this generates an error (zero rollback error). How to change the font and color of the navigation bar button? Feels like I'm not changing the right things ...

+10
ios swift swift2 uinavigationbar
source share
7 answers

If you want to set the same color on the button bar implicitly, and then in appdelegate in didfinishlaunchingwithoption write,

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

Update:

put it 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 helps :)

+14
source share

Swift 3.0 answer (based on Leo'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) 

It works with pleasure for those who have already managed to configure other parts of their navigation bars, but not the "Back" button!

+11
source share

I think you should change it in vc before your actual vc. See: 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.

+4
source share

create a custom button and make it as you wish and add an action to return.

 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) } 
+3
source share

Use the following code:

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

change color according to your needs

+2
source share

Swift 4

in AppDelegate.swift

 UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal) 
+2
source share

in quick 4.2

 to change back button color self.navigationController?.navigationBar.tintColor = .white 
-one
source share

All Articles