46

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 the warning "setStatusBarHidden is deprecated in iOS 9.0, Use -[UIViewController prefersStatusBarHidden". If I just replace 'setStatusBarHidden' with 'prefersStatusBarHidden', I get 'instance method not found'. Can someone please suggest me how to solve this problem?

devops_engineer
  • 599
  • 1
  • 5
  • 13
  • `prefersStatusBarHidden` is a method of UIViewController objects, not UIApplication. So you should set it on each UIViewController. – Kegluneq Jun 29 '15 at 11:34
  • @user1963877 Thank you. As I am new to iOS programming, can you please let me know how to set it on each UIViewController. 'applicationName' is of type UIApplication. – devops_engineer Jun 29 '15 at 12:13

6 Answers6

57

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.
Nilesh Patel
  • 6,318
  • 1
  • 26
  • 40
  • 3
    How is this the solution? preferStatusBarHidden existed before iOS 9. It can only be applied to the view controller so if you want to more dynamically hide or show the status bar how do you handle that in iOS 9? How would you for example show or hide the status bar based on the user touching a button on the screen? – Polar Bear Jul 10 '15 at 00:31
  • @PolarBear You can apply logic like this, -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { shouldHideStatusBar = (shouldHideStatusBar)? NO: YES; [self setNeedsStatusBarAppearanceUpdate]; } – Nilesh Patel Jul 10 '15 at 05:49
  • You can add this code to your button event as well. – Nilesh Patel Jul 10 '15 at 05:49
  • 3
    @NileshPatel Thanks for the help here. I ended up figuring out how to correctly change it just on my root controller with your idea. Thanks again. One tip, make sure to add to your info.plist "View controller-based status bar appearance" set to YES otherwise things just don't seem to work. – Polar Bear Aug 18 '15 at 03:54
  • How can the act of it hiding be animated? – Max Phillips Nov 01 '15 at 00:41
6

prefersStatusBarHidden is available from iOS 7+.

Use this in Your UIViewController class

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

enter image description here

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.

Jack
  • 13,571
  • 6
  • 76
  • 98
5

you have to add method in yourViewController.m

- (BOOL)prefersStatusBarHidden {

   return NO;
}
Gaurav Patel
  • 957
  • 1
  • 6
  • 16
4

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

Adam Smaka
  • 5,977
  • 3
  • 50
  • 55
2

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)

oozoofrog
  • 166
  • 9
1

Swift 3 with Xcode 8.3.3

1) Add a row in you Info.plist. enter image description here

2) In your ViewController ViewDidLoad() override add:

 UIApplication.shared.isStatusBarHidden = true
Codetard
  • 2,441
  • 28
  • 34
  • 2
    'isStatusBarHidden' is also deprecated – fishinear Sep 14 '18 at 20:09
  • That’s right. What I did next was to add a property in info.plist called ‘View Controller based statusbar..’ with Boolean ‘NO’ and in project General setting I changed ‘status bar type’ to ‘light’. – Codetard Sep 15 '18 at 02:11