问题描述
我以前在项目中使用setStatusBarStyle
,它可以正常工作,但是已弃用,所以我使用preferredStatusBarStyle
,但没有用.知道我已经:
I used to use setStatusBarStyle
in my project and it works fine, but it is deprecated so I use preferredStatusBarStyle
, that didn't work. knowing that I've:
- 调用方法setNeedsStatusBarAppearanceUpdate.
- 在info.plist中将基于视图控制器的状态栏外观"设置为否"
-
覆盖该功能
- Call the method setNeedsStatusBarAppearanceUpdate.
- Set "View controller-based status bar appearance" to NO in info.plist
Override the function
- (UIStatusBarStyle)preferredStatusBarStyle{返回UIStatusBarStyleLightContent;}
- (UIStatusBarStyle)preferredStatusBarStyle{return UIStatusBarStyleLightContent;}
此函数未调用
注意:我正在使用导航控制器.
Note: I'm using navigation controller.
推荐答案
如果要设置状态栏样式,请在.plist
文件中将应用程序级别设置为UIViewControllerBasedStatusBarAppearance
至NO
.并在appdelegate
> didFinishLaunchingWithOptions
中添加以下ine(通过编程,您可以从应用程序委托中进行操作).
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance
to NO
in your .plist
file. And in your appdelegate
> didFinishLaunchingWithOptions
add following ine (programatically you can do it from app delegate).
目标C
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
快速
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.statusBarStyle = .lightContent
return true
}
如果要设置状态栏样式,请在视图控制器级别执行以下步骤:
if you want to set status bar style, at view controller level then follow these steps:
- 如果仅需要在UIViewController级别上设置状态栏样式,请在
.plist
文件中将UIViewControllerBasedStatusBarAppearance
设置为YES
. -
在viewDidLoad中添加功能-
setNeedsStatusBarAppearanceUpdate
- Set the
UIViewControllerBasedStatusBarAppearance
toYES
in the.plist
file, if you need to set status bar style at UIViewController level only. In the viewDidLoad add function -
setNeedsStatusBarAppearanceUpdate
在视图控制器中覆盖preferredStatusBarStyle.
override preferredStatusBarStyle in your view controller.
目标C
- (void)viewDidLoad
{
[super viewDidLoad];
[self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
快速
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
根据状态栏样式设置级别设置.plist的值.
Set value of .plist according to status bar style setup level.
您可以在应用程序启动或视图控制器的viewDidLoad期间为状态栏设置背景颜色.
You can set background color for status bar during application launch or during viewDidLoad of your view controller.
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
}
}
结果如下:
Here is result:
这篇关于preferredStatusBarStyle无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!