我发现很难以编程方式更改状态栏样式。
我看到了如何使用(在ViewController.swift中)组合为每个ViewController静态设置它:
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.default
}
和(在info.plist中):
View controller-based status bar appearance = YES
...
我想随时更改它!
最佳答案
经过大量的挖掘找到了答案!
设置(在info.plist中):
View controller-based status bar appearance = NO
并删除(在ViewController.swift中):
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.default
}
...
现在您可以使用(在ViewController.swift中):
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)
并且,要最初为每个ViewController设置样式,请使用viewDidAppear:
override func viewDidAppear(_ animated: Bool) {
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: false)
}