How to programmatically hide and show status bar in iOS 13?

做~自己de王妃 提交于 2020-01-11 13:23:30

问题


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];
    }
}

Getting following error for iOS 13

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?


回答1:


If you want to show/hide the status bar on the different View Controllers you need to:

  1. Add View controller-base status bar appearance option in your Info.plist and set it to YES
  2. 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 
}
  • You could find more verbose explanation here Here

  • Also in the Apple Documentation for UIStatusBarManager you can find the following quote:

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.



来源:https://stackoverflow.com/questions/58134590/how-to-programmatically-hide-and-show-status-bar-in-ios-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!