setStatusBarHidden is deprecated in iOS 9.0

后端 未结 6 2145
梦毁少年i
梦毁少年i 2020-12-14 00:00

I am upgrading my code from iOS 8 to iOS 9. I have a code snippet in my program [[UIApplication applicationName] setStatusBarHidden:YES];.

I am getting

相关标签:
6条回答
  • 2020-12-14 00:40

    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.

    0 讨论(0)
  • 2020-12-14 00:42

    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
    
    0 讨论(0)
  • 2020-12-14 00:44

    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)

    0 讨论(0)
  • 2020-12-14 00:45

    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.
    0 讨论(0)
  • 2020-12-14 00:46

    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

    0 讨论(0)
  • 2020-12-14 01:02

    you have to add method in yourViewController.m

    - (BOOL)prefersStatusBarHidden {
    
       return NO;
    }
    
    0 讨论(0)
提交回复
热议问题