如何在iOS 13中以编程方式隐藏和显示状态栏? [英] How to programmatically hide and show status bar in iOS 13?
问题描述
我做了以下常见的隐藏和再次显示状态栏的方法.在iOS 13之前,它可以正常工作,但在具有iOS 13或更高版本的设备上运行它时,我却遇到了崩溃的情况.
I have made following common method for hiding and showing again status bar. It works fine before iOS 13, but I am getting following crash while I run it for device having iOS 13 or greater.
+(void)showStatusBar:(BOOL)show
{
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
[[UIApplication sharedApplication] setStatusBarHidden:!show withAnimation:UIStatusBarAnimationNone];
}
}
iOS 13出现以下错误
由于未捕获的异常而终止应用程序 "NSInternalInconsistencyException",原因:应用程序名为-statusBar或 UIApplication上的-statusBarWindow:由于不再有状态栏或状态栏窗口,因此必须更改此代码.使用 而是在窗口场景上使用statusBarManager对象.'
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'
我该怎么做才能仅对某些视图控制器隐藏和显示状态栏?
What can I do to hide and show status bar for some view controllers only?
推荐答案
如果要在不同的View Controller上显示/隐藏状态栏,则需要:
If you want to show/hide the status bar on the different View Controllers you need to:
- 在Info.plist中添加查看控制器基础状态栏外观选项,并将其设置为 YES
- 在每个要显示/隐藏状态栏的View Controller中覆盖
var prefersStatusBarHidden: Bool
- Add View controller-base status bar appearance option in your Info.plist and set it to YES
- Override
var prefersStatusBarHidden: Bool
in each View Controller where you want to have the status bar shown/hidden
override var prefersStatusBarHidden: Bool {
return true
}
如果要动态显示/隐藏它(例如,点击按钮后),则可以执行以下操作:
If you want to show/hide it dynamically (ex. after tapping on button), you could do something like this:
var statusBarHidden = true {
didSet {
setNeedsStatusBarAppearanceUpdate()
}
}
override var prefersStatusBarHidden: Bool {
return statusBarHidden
}
-
您可以在此处找到更多详细的说明还可以在 Apple文档中找到
UIStatusBarManager
以下引用:Also in the Apple Documentation for
UIStatusBarManager
you can find the following quote:您不使用此对象来修改状态栏的配置.相反,您可以分别为每个UIViewController对象设置状态栏配置.例如,要修改状态栏的默认可见性,请覆盖视图控制器的preferredsStatusBarHidden属性.
You do not use this object to modify the configuration of the status bar. Instead, you set the status bar configuration individually for each of your UIViewController objects. For example, to modify the default visibility of the status bar, override the prefersStatusBarHidden property of your view controller.
这篇关于如何在iOS 13中以编程方式隐藏和显示状态栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!