【问题标题】:Setting status bar and navigation bar to be the same color将状态栏和导航栏设置为相同颜色
【发布时间】:2019-03-14 01:38:32
【问题描述】:

您好,我想将状态栏设置为与UINavigationBar 完全相同的颜色。我在下面显示的代码中使用了完全相同的颜色,但正如您从图像中看到的那样,它们不是相同的蓝色。有什么方法可以使它们相同。我尝试谷歌但找不到任何解决方案。

UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 13/225, green: 71/255, blue:161/255, alpha: 1.0)

UINavigationBar.appearance().barTintColor = UIColor(red: 13/225, green: 71/255, blue:161/255, alpha: 1.0)

【问题讨论】:

  • 您是在使用导航控制器还是只是将导航栏添加到某个视图控制器?使用导航控制器,无需尝试设置状态栏的颜色。默认会匹配导航控制器的导航栏。
  • 试试这个 - navigationBar.isTranslucent = false
  • @Shezad,它有效!您可以将此作为解决方案发布,我会将其标记为正确!

标签: swift statusbar navigationbar


【解决方案1】:

尝试将半透明改为假

navigationBar.isTranslucent = false

【讨论】:

    【解决方案2】:

    您可以将以下函数添加到AppDelegate.swift 文件中

    func changeStatusBarColor(_ color: UIColor) {
            guard let statusBar = (UIApplication.shared.value(forKey: "statusBarWindow") as AnyObject).value(forKey: "statusBar") as? UIView else {
                return
            }
            statusBar.backgroundColor = color
        }
    

    并在didFinishLaunchingWithOptions 方法中使用,如下所示

    self.changeStatusBarColor(UIColor.black) //pass the color you want to set
    

    已编辑

    您可以在AppDelegate.swift 文件中使用共享实例

    static let shared = AppDelegate()
    

    之后,每当您设置导航背景颜色时,只需将其传递给 changeStatusBarColor 方法

    使用以下方法设置与导航背景颜色相同

    AppDelegate.shared.changeStatusBarColor((self.navigationController?.navigationBar.backgroundColor)!)
    

    【讨论】:

    • 我知道如何设置状态栏的颜色。我需要的是为状态栏和导航栏获得完全相同的颜色。
    • @DarylWong 好的,我明白了你的意思,我编辑了我的答案,请检查一下,让我知道它是否适合你
    【解决方案3】:
     self.navigationController?.navigationBar.tintColor = UIColor.blue 
     self.navigationController?.navigationBar.barTintColor = UIColor.blue
    

    我在 xcode 10.1、swift 4.2 上运行。希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      self.navigationController.navigationBar.barTintColor = UIColor.yourColor

      【讨论】:

        【解决方案5】:

        如果您希望状态栏与导航栏匹配,则不必设置状态栏颜色。注释掉以下行可以解决我的环境中的这个问题。

        Screenshot of the results

        // UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 13/225, green: 71/255, blue:161/255, alpha: 1.0)
        
        UINavigationBar.appearance().barTintColor = UIColor(red: 13/225, green: 71/255, blue:161/255, alpha: 1.0)
        

        或者,您可以执行以下操作:

        UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 13/225, green: 71/255, blue:161/255, alpha: 0.0)
        
        UINavigationBar.appearance().barTintColor = UIColor(red: 13/225, green: 71/255, blue:161/255, alpha: 0.0)
        

        【讨论】:

        • 如果你设法让状态栏的颜色与导航栏的颜色完全相同,你能发布截图吗?我没有设法让它工作。
        猜你喜欢
        • 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
        相关资源
        最近更新 更多