【问题标题】:Status bar and navigation bar does not seems to have the same color iOS Swift状态栏和导航栏好像没有 iOS Swift 一样的颜色
【发布时间】:2017-02-15 14:55:26
【问题描述】:

我想将状态栏颜色设置为与导航栏相同的颜色。当我尝试将导航栏和状态栏设置为相同颜色时,导航栏总是以比状态栏更浅的颜色显示。

这就是我想要的:

我的结果:

AppDelegate 中的代码:

状态栏:

UIApplication.sharedApplication().statusBarStyle = .Default
    let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
    if statusBar.respondsToSelector(Selector("setBackgroundColor:")) 
{
      statusBar.backgroundColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
      statusBar.tintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
}

导航栏:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
        UINavigationBar.appearance().backgroundColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
        UIApplication.sharedApplication().statusBarHidden = false
        UIApplication.sharedApplication().statusBarStyle = .Default

谁能给我任何关于如何解决这个问题的建议。

提前致谢!

【问题讨论】:

  • 用相同的导航颜色设置视图的颜色,这会做你想要的
  • 这不是你使用whiteColor作为状态栏上UINavigationBarUIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)的颜色那么简单
  • 请更新到 Swift 3。
  • 是的,这是下一步,当前版本是 Swift 2.3。升级到 Swift 3.0 后,某些功能不再起作用,这就是我将其留在 Swift 2.3 中的原因。但我们已经忙于解决问题。由于我们需要尽快将其交出,因此这是将其留在 Swift 2.3 中的唯一解决方案。

标签: swift uinavigationbar uistatusbar


【解决方案1】:

警告!不要在 iOS 13 或更高版本中执行任何此操作,绝对不要在 iOS 15 或更高版本中执行此操作。这是很久以前的过时代码!


我能够得到想要的结果:

这是我使用的代码(Swift 3):

let app = UINavigationBar.appearance()
// nav bar color => your color
app.barTintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
app.isTranslucent = false
// status bar text => white
app.barStyle = .black
// nav bar elements color => white
app.tintColor = .white
app.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]

您代码中的这一行是非法的,可能会导致您的应用被 App Store 禁止:

let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView

对于状态栏,现在 iOS 中它的颜色是clear。没有必要设置它的颜色,你也不应该这样做。导航栏在状态栏后面向上延伸,因此它们将始终具有相同的颜色,因为您看到的始终是相同的界面对象,即导航栏。

至于设置导航栏的颜色,别忘了默认是半透明的。如果不使其不透明,您将无法准确设置其颜色。此外,您不应该设置它的backgroundColor。你应该设置它的barTintColor 或者给它一个背景图片(这是最好地控制它的颜色的方法)。

【讨论】:

  • 浏览我看到的使用那段代码的其他论坛是我可以更改状态栏颜色的唯一方法。由于在某些部分中,状态必须以不同的颜色显示。
【解决方案2】:

对我来说,关键是改变 isTranslucent 属性:

navigationBar.isTranslucent = false
navigationBar.barTintColor = PlateColors.mainRed
navigationBar.backgroundColor = PlateColors.mainRed

【讨论】:

    【解决方案3】:

    要达到预期的效果,请将状态栏样式设置为默认值,并将 UINavigationBar.appearance().barTintColor 设置为所需的颜色。

    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    UINavigationBar.appearance().barTintColor = UIColor(red: 43/255.0, green: 79/255.0, blue: 133/255.0, alpha: 1.0)
    UIApplication.sharedApplication().statusBarStyle = .Default
    

    【讨论】:

    • 谢谢我用你的代码得到了完全想要的结果!
    【解决方案4】:

    基于@matt 建议在状态栏下方扩展导航栏,导航栏必须是不透明的:

        let navigationBar = navigationController?.navigationBar
        navigationBar?.isOpaque = true
        navigationBar?.setBackgroundImage(UIImage(color: UIColor(white: 0, alpha: 0.5)), for: .default)
        navigationBar?.shadowImage = UIImage()
    

    extension UIImage {
    
        convenience init?(color: UIColor) {
            let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
            UIGraphicsBeginImageContext(rect.size)
            let context = UIGraphicsGetCurrentContext()
    
            context?.setFillColor(color.cgColor)
            context?.fill(rect)
    
            let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            guard let cgImage = image?.cgImage else { return nil }
            self.init(cgImage: cgImage)
        }
    }
    

    【讨论】:

      【解决方案5】:

      对于像我一样(Xcode 13.1、iOS 15.1)这样晚一点来到这里的人来说,这可能会有所帮助:

      In iOS13 the status bar background colour is different from the navigation bar in large text mode

      这就是定义standardAppearancescrollEdgeAppearance 的外观并设置它们各自的背景颜色。

      就个人而言,甚至不需要以编程方式解决此问题: Navigation Bar Attributes

      【讨论】:

        【解决方案6】:

        我也遇到了同样的问题,发现不是状态栏的问题,而是导航栏的问题。如果将导航栏的背景颜色设置为与简单的 UIView 相同,则显示的真实颜色会有所不同。使导航栏的颜色与另一个视图相同:

        代码错误

        navigationBar.backgroundColor = .blue
        view.backgroundColor = .blue
        

        正确的代码

        navigationBar.isTranslucent = false
        navigationBar.barTintColor = .blue
        view.backgroundColor = .blue
        

        在这种情况下,它们的颜色将相同。

        【讨论】:

          猜你喜欢
          • 2017-03-26
          • 2019-04-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-23
          • 1970-01-01
          相关资源
          最近更新 更多