【问题标题】:How to change my navigation title font and color both together? [duplicate]如何同时更改我的导航标题字体和颜色? [复制]
【发布时间】:2018-10-01 16:59:49
【问题描述】:

我想更改自定义导航栏的背景颜色以及标题字体颜色、字体样式和字体大小。所以我在 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 但在我的情况下不起作用。怎么办?

【问题讨论】:

  • 您的代码看起来不错。检查它是否在其他地方改变了它。
  • 您没有阅读@matt 对exact 相同问题的回答。
  • @matt 的回答无效。
  • 您确定@matt 的回答有效吗?先生。
  • 因为它向我显示这样的错误:Use of unresolved identifier 'NSfontAttributedName

标签: ios swift


【解决方案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
    ]

【讨论】:

  • 这是为导航栏设置“样式”的完美方式,但你应该提到,如果导航栏已经显示,这将不起作用。它将更改将要显示的下一个导航栏的外观。所以这段代码必须放在一个完美的地方工作;)。
  • 谢谢@danypata,我编辑了我的答案。
【解决方案2】:

试试这个来同时改变导航标题的字体和颜色:

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: **YourFont**, NSAttributedStringKey.foregroundColor:**Your Color**]

【讨论】:

  • 谢谢它的工作。
猜你喜欢
  • 2023-03-09
  • 2019-05-12
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 2016-05-31
  • 2014-07-30
相关资源
最近更新 更多