我需要从AppDelegate更改UINavigationBar后退栏按钮文本,以将更改应用到我的应用程序中的所有视图.
UINavigationBar.appearance().titleTextAttributes = [ NSFontAttributeName: UIFont(name: "MyCustomFont",size: 20)! ]
但我不知道如何访问左侧栏按钮对其进行更改.
解决方法
斯威夫特3.0,4.0
只需通过UINavigationItem的扩展即可实现它.根据许多搜索,无法使用app 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 } }
更新:
您可以在didFinishLaunchingWithOptions上更改AppDelegate的后退按钮箭头颜色,
/*It will change back arrow color only if you use backItem.setTitleTextAttributes,else it will change whole text color*/ UINavigationBar.appearance().tintColor = UIColor.orange