状态栏背景颜色不会更改为自定义颜色
Status bar background color does not change to custom color
当我将状态栏背景颜色更改为原生颜色时 UIColor.gray 它会发生变化。
但是当我想使用自定义颜色时,它会变成黑色。
UIApplication.shared.statusBarView?.backgroundColor = UIColor.gray
- 此代码工作正常。状态栏背景色为灰色
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 30/255, green: 30/255, blue: 30/255, alpha: 1)
- 此代码不正确。状态栏背景色为黑色
首先,在 info.plist 文件中设置基于视图控制器的状态栏外观 属性 否。
然后在AppDelegate的didFinishLaunchingWithOptions方法中添加如下代码Class.
extension UIApplication {
var statusBarView: UIView? {
if #available(iOS 13.0, *) {
let tag = 5111
if let statusBar = self.keyWindow?.viewWithTag(tag) {
return statusBar
} else {
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
statusBarView.tag = tag
self.keyWindow?.addSubview(statusBarView)
return statusBarView
}
} else {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
}
return nil}
}
希望对您有所帮助。
代码没有错,只是您提供的颜色是 dark gray
本身。 (R:30,G:30,B:30)
表示 dark gray
,这就是 status bar
中使用的颜色,将此颜色更改为其他颜色,例如 (R:202,G:0,B:42)
,它将显示 red
颜色.
尝试
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 202/255, green: 0/255, blue: 42/255, alpha: 1)
希望这对您有所帮助 Swift 4. 看起来像是一个 hacky trick 但有效。
您可以在应用程序启动期间或视图控制器的 viewDidLoad 期间设置状态栏的背景颜色。
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
这是结果:
当我将状态栏背景颜色更改为原生颜色时 UIColor.gray 它会发生变化。 但是当我想使用自定义颜色时,它会变成黑色。
UIApplication.shared.statusBarView?.backgroundColor = UIColor.gray
- 此代码工作正常。状态栏背景色为灰色
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 30/255, green: 30/255, blue: 30/255, alpha: 1)
- 此代码不正确。状态栏背景色为黑色
首先,在 info.plist 文件中设置基于视图控制器的状态栏外观 属性 否。
然后在AppDelegate的didFinishLaunchingWithOptions方法中添加如下代码Class.
extension UIApplication {
var statusBarView: UIView? {
if #available(iOS 13.0, *) {
let tag = 5111
if let statusBar = self.keyWindow?.viewWithTag(tag) {
return statusBar
} else {
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
statusBarView.tag = tag
self.keyWindow?.addSubview(statusBarView)
return statusBarView
}
} else {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
}
return nil}
}
希望对您有所帮助。
代码没有错,只是您提供的颜色是 dark gray
本身。 (R:30,G:30,B:30)
表示 dark gray
,这就是 status bar
中使用的颜色,将此颜色更改为其他颜色,例如 (R:202,G:0,B:42)
,它将显示 red
颜色.
尝试
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 202/255, green: 0/255, blue: 42/255, alpha: 1)
希望这对您有所帮助 Swift 4. 看起来像是一个 hacky trick 但有效。
您可以在应用程序启动期间或视图控制器的 viewDidLoad 期间设置状态栏的背景颜色。
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
这是结果: