如何同时更改导航标题的字体和颜色?

时间:2018-10-01 07:44:04

标签: ios swift

我想更改自定义导航栏的背景颜色以及标题字体颜色,字体样式和字体大小。所以我尝试将这段代码放入AppDelegate.swift文件

 UINavigationBar.appearance().barTintColor = UIColor(red: 0/255, green: 162/255, blue: 255/255, alpha: 1.0)
 UINavigationBar.appearance().tintColor = UIColor.white
 let navigationTitleFont = UIFont(name: "Poppins", size: 20)!
 let navigaiontitlecolor = UIColor.white

 UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : navigaiontitlecolor]
 UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.font : navigationTitleFont]  


我得到了背景色。我的标题字体样式和大小都可以。但是我的标题字体颜色不正确。所以我去搜索,我应用了此链接代码     Change Both Title Text Color and Font in all Navigation Bars  但在我的情况下不起作用。该怎么办?

2 个答案:

答案 0 :(得分:1)

下面的代码将全局更改UINavigationBar样式,并删除其底行。只需将它们粘贴到 AppDelegate

中的didFinishLaunchingWithOptions
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor(red: 0/255, green: 162/255, blue: 255/255, alpha: 1.0)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().isTranslucent = true
UINavigationBar.appearance().titleTextAttributes = [
        NSAttributedString.Key.font: UIFont(name: "Poppins", size: 20)!,
        NSAttributedString.Key.foregroundColor: UIColor.white
    ]

答案 1 :(得分:0)

尝试此操作以同时更改导航标题的字体和颜色:

Current.Value.X