23

In my UIViewController, I have:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [self.view sizeToFit];
}

Yet the view looks like this:

enter image description here

I'm sure this code runs. I load the view from a xib. I haven't done anything else to the status bar like change its style. What could be wrong?

Even when I set `application.statusBarHidden = YES" in my app delegate, I see:

enter image description here

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243

6 Answers6

62

In your app's plist, if you have "View controller-based status bar appearance" set to YES, put this code in the view controller in which you hide the status bar:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

Else if "View controller-based status bar appearance" is set to NO, call the following whenever you want to hide the status bar.

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
  • You should have checked this first: [Status bar won't disappear](http://stackoverflow.com/a/17954332/1756131) – DD_ Aug 20 '13 at 09:04
  • This is the way to go after iOS 7 – quarac Sep 02 '14 at 17:49
  • 2
    `[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];` is deprecated can you provide an answer for ios9 @quarac @Rose – Varun Kumar Sep 02 '16 at 15:18
45

if you want to hide Status Bar in your app , Follow this steps :

Step 1 :

enter image description here

Step 2:

enter image description here

Step 3:

Add to your appDelegate didFinishLaunchingWithOptions function

application.statusBarHidden = YES;

so :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
      application.statusBarHidden = YES;
}
Erhan Demirci
  • 4,173
  • 4
  • 36
  • 44
  • 5
    This forces your app into a compatibility mode designed solely to make old apps run as intended on iOS 7 when developers have no time to fix the app for the new way to do things. Instead, you need to implement the prefer* methods in your view controllers. – lhunath Jan 14 '14 at 19:25
  • 3
    @ Erhan Demirci you just implement `-preferStatusBarHidden` on the active view controller like @Rose Perrone suggested. – lhunath Jan 19 '14 at 02:00
  • you can hide in all project with preferStatusBarHidden ? Because I hide status bar in all viewcontroller . – Erhan Demirci Jan 19 '14 at 08:41
  • If each of your VCs want the status bar hidden, they should all explicitly convey that preference by implementing the method (assuming that they're VCs that are put in control of the status bar). – lhunath Jan 20 '14 at 15:55
  • 1
    @lhunath Does using this "compatibility mode" cause any other problems? This seems to be the only good way to hide/show the status bar with animations, as `prefersStatusBarHidden` doesn't seem to support that. – eyuelt May 25 '14 at 22:28
  • @eyuelt You seem to confuse intent and problems. A compatibility mode is not "the only good way" to do anything. If the new VC methods aren't doing what you intend, you should look into why that is. The new method most certainly supports animation. This isn't the place to diagnose your issue though, make a new question if you can't figure it out. – lhunath May 27 '14 at 03:32
  • Here's one example of a problem: I have a view controller which sometimes has a status bar and sometimes doesn't...that seems to mean I need to do it the 'deprecated' way – plivesey Oct 28 '14 at 04:32
  • @ihunath said: "If the new VC methods aren't doing what you intend, you should look into why that is." Or you could spend the 30 hours on sleep and other work that actually matters. If all you want is the status bar gone, burn it with fire I say. – Kendall Helmstetter Gelner Dec 11 '14 at 05:55
12

That's because iOS 7 has changed the way it deals with the status bar.

Setting UIViewControllerBasedStatusBarAppearance to NO on your app Info.plist should work.

daniel.gindi
  • 3,457
  • 1
  • 30
  • 36
Marcelo
  • 9,916
  • 3
  • 43
  • 52
  • 3
    Don't forget to point out that this is effectively disabling the new way to do things and reverting to a compatibility mode. – lhunath Jan 14 '14 at 19:26
11

You can show/hide your app status bar using the following code (Works on IOS 7 - IOS 8 and IOS 9):

in your project .h file add this boolean:

BOOL isShowStatus;

And in .m file add this:

//To show the status bar:
-(void)showTheStatusBar
{
    isShowStatus = YES;
    [self setNeedsStatusBarAppearanceUpdate];
}

//And to hide the status bar:
-(void)hideTheStatusBar
{
    isShowStatus = NO;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (BOOL)prefersStatusBarHidden {
    return !isShowStatus;
}

Simply call it from anywhere, didload for example:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //To show the status bar:

    [self showTheStatusBar];

    //Or to hide it:

    [self hideTheStatusBar];
}
Jouhar
  • 288
  • 4
  • 9
5

For me it works fine:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

ALWAYS in the root view. If you are doing that in a subview will not work because status bar visibility will be taken from parent view.

-2

Try adding this after you hide the status bar:

 [self.view setFrame:[self.view bounds]];

In your appdelegate.m in didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
      application.statusBarHidden = YES;
}

When I run your code:

enter image description here

Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70