【问题标题】:Different colours for status bar and for navigation bar but actually they should be the same状态栏和导航栏的颜色不同,但实际上它们应该相同
【发布时间】:2018-08-28 05:39:42
【问题描述】:

我有这个扩展,它允许我拥有一些 UIView 属性(例如 backgroundColor):

extension UIApplication {
    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }
}

然后我在 application(didFinishLaunchingWithOptions:) 中编写这段代码

UIApplication.shared.statusBarView?.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
UINavigationBar.appearance().barTintColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)

你可以看到颜色是一样的,但是结果很奇怪,因为实际上它们是不同的:

看到两种绿色之间的区别了吗? 为什么会出现这种差异?

谢谢!

【问题讨论】:

  • 谢谢@SandeepBhandari,但这是嵌入在 UINavigationController 中的。
  • 该扩展程序中的代码很脆弱,可能会在未来的某些 iOS 更新中崩溃。
  • @rmaddy 是的,我也这么认为,但我怎样才能改进它并用字符串删除 kvo?
  • 我从来不需要编写任何代码来更改状态栏的颜色,并且我的应用程序正确地为导航栏和状态栏显示相同的颜色。它只是发生在你身上。
  • 我需要它在向上滑动时隐藏导航栏

标签: ios swift statusbar navigationbar


【解决方案1】:

尝试设置以下值:

UINavigationBar.appearance().shadowImage = UIImage()

如果这没有帮助,请尝试使用以下命令(我在我的应用程序中使用它们将所有内容设置为白色背景):

UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().isTranslucent = false
UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()
UITabBar.appearance().backgroundColor = UIColor.white

【讨论】:

  • 谢谢。但这对我没有帮助。
  • INavigationBar.appearance().isTranslucent = false 这对我来说是个把戏!谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多