【问题标题】:different navigation bar background color between iOS 10 & iOS 11iOS 10 和 iOS 11 导航栏背景颜色不同
【发布时间】:2018-10-17 20:40:40
【问题描述】:

我将导航栏颜色设置为 #479F46 ,在 iOS 10 中显示为 #5DA15D 但在 iOS 11 中显示为 #4D9D4E

  • 我做错了什么?
  • 以及如何解决这个问题?

我在应用委托中使用的代码

func setupNavigationController() {
    UINavigationBar.appearance().barTintColor = UIColor.colorWithHexString(hexStr: "479F46")
    UINavigationBar.appearance().tintColor = UIColor.white  
    let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
    UINavigationBar.appearance().titleTextAttributes = textAttributes
}

编辑 1

我使用isTranslucent = false 作为我的导航控制器对象,

nvc?.navigationBar.isTranslucent = false

但这似乎还不够,我添加了UINavigationBar.appearance().isTranslucent = false 正如 llb 建议的那样

现在两个视图操作系统版本都将颜色显示为#4D9D4E

我需要他们都显示为#479F46

【问题讨论】:

  • 假设半透明不是问题,iOS 12 会显示什么?
  • 您的colorWithHexString 中可能存在错误。谁知道它是做什么的?你没有显示它。
  • 我又错过了一件事。您还应该按照@matt 的建议设置UINavigationBar.appearance().backgroundColor = <Your color> 并删除barTintColor

标签: ios swift xcode uinavigationbar


【解决方案1】:

这是因为半透明。将其设置为假。另外,正如@matt 建议的那样,不要使用barTintColor。改为设置backgroundColor

UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().backgroundColor = <Your color>

【讨论】:

  • 既然这可能不是问题,为什么要发布它作为答案?为什么不在评论中问呢?
  • 修复了答案。感谢您的建议。
【解决方案2】:

如果您想完全控制导航栏的颜色,请不要尝试使用barTintColor。色调颜色是 tint。您想指定实际颜色

只有一种方法可以做到这一点。 没有barTintColor没有半透明。然后给导航栏一个背景图片,由所需颜色的矩形组成。

例子:

let sz = CGSize(20,20)
let c = // some UIColor
let r = UIGraphicsImageRenderer(size:sz)
self.navbar.setBackgroundImage( r.image { ctx in
    c.setFill()
    ctx.fill(CGRect(origin:.zero, size:sz))
}, for:.any, barMetrics: .default)

【讨论】:

    【解决方案3】:

    这与颜色配置文件有关,请确保您使用的是正确的配置文件

    查看这篇文章了解更多详情:

    https://lembergsolutions.com/blog/how-get-right-color-ios-detailed-instruction

    【讨论】:

      猜你喜欢
      • 2015-05-14
      • 2014-08-14
      • 1970-01-01
      • 2016-12-30
      • 2020-08-12
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      相关资源
      最近更新 更多