ios – 使用Swift从AppDelegate更改UINavigationBar后退按钮文本和字体

前端之家收集整理的这篇文章主要介绍了ios – 使用Swift从AppDelegate更改UINavigationBar后退按钮文本和字体前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要从AppDelegate更改UINavigationBar后退栏按钮文本,以将更改应用到我的应用程序中的所有视图.

我使用以下方法更改了标题字体样式:

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

但我不知道如何访问左侧栏按钮对其进行更改.

解决方法

斯威夫特3.0,4.0

只需通过UINavigationItem的扩展即可实现它.根据许多搜索,无法使用app delegate更改左按钮文本.

  1. extension UINavigationItem{
  2.  
  3. override open func awakeFromNib() {
  4. super.awakeFromNib()
  5.  
  6. let backItem = UIBarButtonItem()
  7. backItem.title = "Hello"
  8.  
  9.  
  10. if let font = UIFont(name: "Copperplate-Light",size: 32){
  11. backItem.setTitleTextAttributes([NSFontAttributeName:font],for: .normal)
  12. }else{
  13.  
  14. print("Font Not available")
  15. }
  16. /*Changing color*/
  17. backItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.green],for: .normal)
  18.  
  19. self.backBarButtonItem = backItem
  20. }
  21.  
  22. }

更新:

您可以在didFinishLaunchingWithOptions上更改AppDelegate的后退按钮箭头颜色,

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

猜你在找的iOS相关文章