override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent;
}
使用上述代码在任何ViewController中为特定的ViewController设置状态栏颜色为白色,在iOS8中对我来说是行不通的。有什么建议吗?使用UIApplication。shared应用方法,在信息中需要更改后颜色更改。Plist为整个应用程序。
// Change the colour of status bar from black to white
UIApplication.sharedApplication().statusBarStyle = .LightContent
我怎么能改变一些必要的和特定的视图控制器的状态栏颜色?
我已经设置了特定的颜色(在RGB格式)使用下面的代码在应用程序委托文件:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
. . .
UIApplication.sharedApplication().statusBarHidden = false
UIApplication.sharedApplication().statusBarStyle = .LightContent
let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
if statusBar.respondsToSelector(Selector("setBackgroundColor:")) {
statusBar.backgroundColor = UIColor.init(red: 0.1, green: 0.27, blue: 0.60, alpha: 1.0)
}
. . .
}
您还需要添加以下关键信息。Plist文件:
查看基于控制器的状态栏外观,布尔值设置为NO
请在“didFinishLaunchingWithOptions launchOptions:”Appdelegate类中使用此代码
UIApplication.shared.statusBarStyle = .lightContent let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView if statusBar.responds(to: #selector(setter: UIView.backgroundColor)){ statusBar.backgroundColor = UIColor.black }
iOS 13
var statusBarView: UIView = UIView()
if #available(iOS 13.0, *) {
let tag:UInt64 = 38482458385
if let statusBar = UIApplication.shared.keyWindow?.viewWithTag(Int(tag)) {
statusBar.backgroundColor = UIColor.red
statusBarView = statusBar
} else {
let statusBar = UIView(frame: UIApplication.shared.statusBarFrame)
statusBar.tag = Int(tag)
UIApplication.shared.keyWindow?.addSubview(statusBar)
statusBarView = statusBar
}
} else {
statusBarView = (UIApplication.shared.value(forKey: "statusBar") as? UIView)!
if statusBarView.responds(to: #selector(setter: UIView.backgroundColor)){
statusBarView.backgroundColor = UIColor.red
}
}
我遵循了这个教程,它对我很有效。然而,我不确定是否有任何警告。
https://coderwall.com/p/dyqrfa/customize-navigation-bar-appearance-with-swift
打开你的信息。Plist和set
UIViewControllerBasedStatusBarAppearance设为false。
在AppDelegate.swift中的第一个函数中,它包含didFinishLaunchingWithOptions,设置你想要的颜色。
UIApplication.sharedApplication()。statusBarStyle = UIStatusBarStyle。LightContent
Swift 3更新*
共享。statusbarstyle = .lightContent