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

标签 ios swift

我需要更改 AppDelegate 中的 UINavigationBar 后退栏按钮文本,以将更改应用到我应用中的所有 Views

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

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

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

最佳答案

swift 3.0,4.0

只需使用UINavigationItemextension 即可实现。根据许多搜索,无法使用应用程序委托(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

关于ios - 使用 Swift 从 AppDelegate 更改 UINavigationBar 后退按钮文本和字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45650840/

相关文章:

ios - 无法将值传递给下一个 UIViewController

ios - 当我们在表格 View 单元格上滑动时如何将图像添加到删除按钮

ios - 无法从 3.0 EAGLContext 创建 CIContext

iphone - SDWebImage 在缓存之前处理图像

iOS Swift 将具有奇怪格式的 JSON 响应转换为字典

ios - 键盘在 ios 中显示方向错误

ios - 动态 viewForHeaderInSection 和多个 cellForRowAtIndexPath

swift - 如何在 iOS 8 中以编程方式隐藏状态栏

ios - 使用 Swift 计算以公里为单位的距离

swift - 使用 NSRegularExpression 结果对字符串进行子字符串化的正确方法是什么?