Change text and font of UINavigationBar button back from AppDelegate using Swift

I need to change the text of the UINavigationBar control panel button from AppDelegate to apply the changes to all Views in my application.

I changed the header font style using:

 UINavigationBar.appearance().titleTextAttributes = [ NSFontAttributeName: UIFont(name: "MyCustomFont", size: 20)! ] 

But I do not know how to access the left pane to make changes.

+5
source share
4 answers

Swift 3.0.4.0

You can simply achieve this with the extension UINavigationItem . According to many searches, there is no way to change the text of the left button with the application delegate.

 extension UINavigationItem{ override open func awakeFromNib() { super.awakeFromNib() let backItem = UIBarButtonItem() backItem.title = "Hello" if let font = UIFont(name: "Copperplate-Light", size: 32){ backItem.setTitleTextAttributes([NSFontAttributeName:font], for: .normal) }else{ print("Font Not available") } /*Changing color*/ backItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.green], for: .normal) self.backBarButtonItem = backItem } } 

Update:

You can change the color of the back button arrow from AppDelegate to didFinishLaunchingWithOptions ,

  /*It will change back arrow color only if you use backItem.setTitleTextAttributes, else it will change whole text color*/ UINavigationBar.appearance().tintColor = UIColor.orange 
+7
source

It will help you

 UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "MyCustomFont", size: 20)!, for: .normal) 
+1
source

Try the following:

  • Subclass UINavigationController and use it. Then make your viewDidLoad as follows: (change the attributes to suit your needs)

     // // NavConViewController.swift // customattributedbackbtn // // Created by Glenn Posadas on 8/12/17. // Copyright © 2017 Glenn Posadas. All rights reserved. // import UIKit class NavConViewController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() let font = UIFont(name: "Helvetica-Light", size: 12)! var attributes: [String : Any] = [NSFontAttributeName : font] attributes[NSForegroundColorAttributeName] = UIColor.black UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self, NavConViewController.self]).setTitleTextAttributes(attributes, for: .normal) } } 

Output Example:

Default

enter image description here

Attributed

enter image description here

+1
source

To change the color of an image, you can either use a font file, or change its color, or use an image with the color you want.

 let yourBackButtonIcon = //YourImage here let navigationBar = UINavigationBar.appearance() navigationBar.backIndicatorImage = yourBackButtonIcon navigationBar.backIndicatorTransitionMaskImage = yourBackButtonIcon 

To change the color of the button caption text

 navigationBar.titleTextAttributes = [NSFontAttributeName: yourFont, NSForegroundColorAttributeName: yourColor] 

Note: -

The above code should be inside the class AppDelegate applicationDidFinishLaunching method

0
source

Source: https://habr.com/ru/post/1270814/


All Articles