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
我怎么能改变一些必要的和特定的视图控制器的状态栏颜色?
我可以给你一个更简单的方法,
就像苹果文档说的那样,在viewDidLoad中调用setNeedsStatusBarAppearanceUpdate,
如果视图控制器的状态栏属性(如隐藏/不隐藏状态或样式)发生变化,则调用此方法。如果在动画块中调用此方法,则更改将与动画块的其余部分一起动画化。
实现preferredStatusBarStyle返回您的首选类型。
我在iOS 10.1系统中使用了这个功能。
Objective - C
[self setNeedsStatusBarAppearanceUpdate];
-(UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
斯威夫特
setNeedsStatusBarAppearanceUpdate()
var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
我很惊讶没有人指出这一点。无论如何,喜欢:)
Swift 4.2解决方案与NavigationController
第一步:
打开你的信息。并插入一个名为“View controller based status bar appearance”或UIViewControllerBasedStatusBarAppearance的新键到YES,让每个VC使用自己的状态属性。
第二步
在每个VC中,像这样重写preferredStatusBarStyle属性:
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent //.default for black style
}
最后一步
重写自定义NavigationController类中的preferredStatusBarStyle属性:
class NavigationController : UINavigationController {
override var preferredStatusBarStyle : UIStatusBarStyle {
if let topVC = viewControllers.last {
//return the status property of each VC, look at step 2
return topVC.preferredStatusBarStyle
}
return .default
}
我已经设置了特定的颜色(在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
我可以给你一个更简单的方法,
就像苹果文档说的那样,在viewDidLoad中调用setNeedsStatusBarAppearanceUpdate,
如果视图控制器的状态栏属性(如隐藏/不隐藏状态或样式)发生变化,则调用此方法。如果在动画块中调用此方法,则更改将与动画块的其余部分一起动画化。
实现preferredStatusBarStyle返回您的首选类型。
我在iOS 10.1系统中使用了这个功能。
Objective - C
[self setNeedsStatusBarAppearanceUpdate];
-(UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
斯威夫特
setNeedsStatusBarAppearanceUpdate()
var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
我很惊讶没有人指出这一点。无论如何,喜欢:)
警告
'statusBarStyle'的Setter在iOS 9.0中已弃用:
UIApplication.shared.statusBarStyle = .default
所以我的解决方案是这样的:
从导航控制器做一个扩展:
extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
if let topViewController = presentedViewController{
return topViewController.preferredStatusBarStyle
}
if let topViewController = viewControllers.last {
return topViewController.preferredStatusBarStyle
}
return .default
}
}
如果你有一个viewController它会有另一个样式而不是app的样式,你可以做这个
var barStyle = UIStatusBarStyle.lightContent
override var preferredStatusBarStyle: UIStatusBarStyle{
return barStyle
}
假设你的app状态样式是。default,你希望这个屏幕是。lightcontent
所以barStyle会把。lightContent作为它的默认值,这将改变状态栏的样式为lightContent,然后确保当viewWillDisappear再次改变barStyle为应用程序状态栏的样式,在我们的例子中是。default。
这对我很有用
请在“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
}
}