如何以编程方式在 iOS 13 中隐藏和显示状态栏?
How to programmatically hide and show status bar in iOS 13?
我做了如下常用的隐藏和显示状态栏的方法。它在 iOS 13 之前工作正常,但是当我 运行 它用于具有 iOS 13 或更大的设备时,我遇到了崩溃。
+(void)showStatusBar:(BOOL)show
{
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
[[UIApplication sharedApplication] setStatusBarHidden:!show withAnimation:UIStatusBarAnimationNone];
}
}
iOS13
出现以下错误
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.'
我怎样才能仅隐藏和显示某些视图控制器的状态栏?
如果你想show/hide不同视图控制器上的状态栏,你需要:
- 在您的 Info.plist 中添加 View controller-base status bar appearance 选项并将其设置为 YES
- 在您想要状态栏的每个视图控制器中重写
var prefersStatusBarHidden: Bool
shown/hidden
override var prefersStatusBarHidden: Bool {
return true
}
如果您想 show/hide 动态地(例如在点击按钮之后),您可以这样做:
var statusBarHidden = true {
didSet {
setNeedsStatusBarAppearanceUpdate()
}
}
override var prefersStatusBarHidden: Bool {
return statusBarHidden
}
你可以在这里找到更详细的解释 Here
此外,在 Apple Documentation 中,对于 UIStatusBarManager
,您可以找到以下引用:
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 之前工作正常,但是当我 运行 它用于具有 iOS 13 或更大的设备时,我遇到了崩溃。
+(void)showStatusBar:(BOOL)show
{
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
[[UIApplication sharedApplication] setStatusBarHidden:!show withAnimation:UIStatusBarAnimationNone];
}
}
iOS13
出现以下错误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.'
我怎样才能仅隐藏和显示某些视图控制器的状态栏?
如果你想show/hide不同视图控制器上的状态栏,你需要:
- 在您的 Info.plist 中添加 View controller-base status bar appearance 选项并将其设置为 YES
- 在您想要状态栏的每个视图控制器中重写
var prefersStatusBarHidden: Bool
shown/hidden
override var prefersStatusBarHidden: Bool {
return true
}
如果您想 show/hide 动态地(例如在点击按钮之后),您可以这样做:
var statusBarHidden = true {
didSet {
setNeedsStatusBarAppearanceUpdate()
}
}
override var prefersStatusBarHidden: Bool {
return statusBarHidden
}
你可以在这里找到更详细的解释 Here
此外,在 Apple Documentation 中,对于
UIStatusBarManager
,您可以找到以下引用:
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.