使用Swift从AppDelegate更改UINavigationBar后退按钮文本和字体 [英] Change UINavigationBar back button text and font from AppDelegate using Swift

查看:330
本文介绍了使用Swift从AppDelegate更改UINavigationBar后退按钮文本和字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 AppDelegate 中更改UINavigationBar后退栏按钮文本,以便对我的应用中的所有视图应用更改。

I need to change UINavigationBar back bar button text from AppDelegate to apply changes to all the Views in my app.

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

I've changed the title font style using:

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

但我不知道如何访问左侧栏按钮它的变化。

But I don't know how to access to the left bar button to make changes on it.

推荐答案

Swift 3.0,4.0

只需使用扩展 UINavigationItem 即可实现。根据许多搜索,无法使用app delegate更改左键文本。

Simply you can achieve it with extension of UINavigationItem. According to many search there is no way to change left button text with 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
    }

}

更新:

您可以从 AppDelegate on 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

这篇关于使用Swift从AppDelegate更改UINavigationBar后退按钮文本和字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆