设置状态栏和导航栏颜色相同
Setting status bar and navigation bar to be the same color
嗨,我想将状态栏设置为与 UINavigationBar
完全相同的颜色。我在下面显示的代码中使用了完全相同的颜色,但正如您从图像中看到的那样,它们不是相同的蓝色。有什么方法可以使它们相同。我尝试 google 但找不到任何解决方案。
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)
如果您希望状态栏颜色与导航栏相匹配,则不必设置状态栏颜色。注释掉以下行可以解决我环境中的这个问题。
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)
self.navigationController?.navigationBar.tintColor = UIColor.blue
self.navigationController?.navigationBar.barTintColor = UIColor.blue
我 运行 在 xcode 10.1,swift 4.2。希望这有帮助。
self.navigationController.navigationBar.barTintColor = UIColor.yourColor
您可以将以下功能添加到 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)!)
尝试将半透明更改为假
navigationBar.isTranslucent = false
嗨,我想将状态栏设置为与 UINavigationBar
完全相同的颜色。我在下面显示的代码中使用了完全相同的颜色,但正如您从图像中看到的那样,它们不是相同的蓝色。有什么方法可以使它们相同。我尝试 google 但找不到任何解决方案。
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)
如果您希望状态栏颜色与导航栏相匹配,则不必设置状态栏颜色。注释掉以下行可以解决我环境中的这个问题。
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)
self.navigationController?.navigationBar.tintColor = UIColor.blue
self.navigationController?.navigationBar.barTintColor = UIColor.blue
我 运行 在 xcode 10.1,swift 4.2。希望这有帮助。
self.navigationController.navigationBar.barTintColor = UIColor.yourColor
您可以将以下功能添加到 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)!)
尝试将半透明更改为假
navigationBar.isTranslucent = false