setStatusBarHidden is deprecated in iOS 9.0

自闭症网瘾萝莉.ら 提交于 2019-11-28 20:06:16

Add below code to your view controller..

 - (BOOL)prefersStatusBarHidden {

   return NO;
}

Note :

  • If you change the return value for this method, call the setNeedsStatusBarAppearanceUpdate method.
  • For childViewController, To specify that a child view controller should control preferred status bar hidden/unhidden state, implement the childViewControllerForStatusBarHidden method.

prefersStatusBarHidden is available from iOS 7+.

Use this in Your UIViewController class

   var isHidden = true{
        didSet{
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }
    override var prefersStatusBarHidden: Bool {
        return isHidden
    }

If you change the return value for this method, call the setNeedsStatusBarAppearanceUpdate() method. To specify that a child view controller should control preferred status bar hidden/unhidden state, implement the childViewControllerForStatusBarHidden method.

you have to add method in yourViewController.m

- (BOOL)prefersStatusBarHidden {

   return NO;
}

Swift 3.1 Xcode 8.2.1

  1. Change in info.plist the row View controller-based status bar appearance and set it to NO

  2. In your target settings tick "Hide Status bar"

Both steps are required

Swift 3 with Xcode 8.3.3

1) Add a row in you Info.plist.

2) In your ViewController ViewDidLoad() override add:

 UIApplication.shared.isStatusBarHidden = true

Here is my swift code for setting status bar hidden and style.

extension UIViewController {

public var privateStatusBarHidden: Bool {
    return statusBarHidden
}

public var privateStatusBarStyle: UIStatusBarStyle {
    return statusBarStyle
}

public func setStatusBarHidden(hidden: Bool, animated: Bool = false) {
    statusBarHidden = hidden
    if animated {
        UIView.animate(withDuration: 0.25, animations: { 
            self.setNeedsStatusBarAppearanceUpdate()
        })
    } else {
        self.setNeedsStatusBarAppearanceUpdate()
    }
}

public func setStatusBar(style: UIStatusBarStyle) {
    statusBarStyle = style
    self.setNeedsStatusBarAppearanceUpdate()
}

    public static func swizzleStatusBarHiddenPropertyForViewController() {
    var original = class_getInstanceMethod(UIViewController.self, #selector(getter: UIViewController.prefersStatusBarHidden))
    var changeling = class_getInstanceMethod(UIViewController.self, #selector(getter: UIViewController.privateStatusBarHidden))
    method_exchangeImplementations(original, changeling)
    original = class_getInstanceMethod(UIViewController.self, #selector(getter: UIViewController.preferredStatusBarStyle))
    changeling = class_getInstanceMethod(UIViewController.self, #selector(getter: UIViewController.privateStatusBarStyle))
    method_exchangeImplementations(original, changeling)

    original = class_getClassMethod(UIViewController.self, #selector(UIViewController.swizzleStatusBarHiddenPropertyForViewController))
    changeling = class_getClassMethod(UIViewController.self, #selector(UIViewController.emptyFunction))
    method_exchangeImplementations(original, changeling)
}

@objc private static func emptyFunction() {}
}

Usage

  • in lauching function

UIViewController.swizzleStatusBarHiddenPropertyForViewController()

  • for hide/show statusBar, in UIViewController

. self.setStatusBar(hidden: true/false)

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