33

I want to show the status bar in my app in all views but one. I have tried modifying the 'status bar is initially hidden' in the plist, i have tried:

[[UIApplication sharedApplication] setStatusBarHidden:YES];

That hides the bar but leaves an ugly blue box where the status bar was (which isn't part of my view, there's nothing blue on there).

I have also tried altering the layout wants full screen and status bar settings in the 'interface builder' bit of Xcode 4.2.

Any suggestions?

EDIT - SORT OF SOLUTION:

I have done it by including:

    -(void)viewWillDisappear:(BOOL)animated{


    [[UIApplication sharedApplication] setStatusBarHidden:YES];

}


-(void)viewDidAppear:(BOOL)animated{


    [[UIApplication sharedApplication] setStatusBarHidden:NO];

}

on every single page that I want the status bar to be on.

It still looks choppy and rubbish because the tab bar appears and reappears each time you switch a view. But i've had enough, worked on this stupid problem for about 5 hours now so this will have to do.

SECOND EDIT -

Fixed the choppyness by including setStatusBarHidden=NO in viewWillAppears. God knows how everything works but it does.

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
  • Is the background of your main view blue? If you get rid of the status bar you need to resize other views to occupy that space. – fearmint Nov 30 '11 at 14:28
  • no it's white and all my views are sized correcty i think.... This view (that I want it hidden in) is part of a tab bar and navigation controller, maybe i have to change something on those in storyboard. – Adam Waite Nov 30 '11 at 15:03
  • If you perform some layout stuff in `viewDidiLoad`, that layout will assume status bar is shown. So it is better to hide it first thing in `viewDidiLoad`. – Yevhen Dubinin Nov 05 '13 at 10:09

9 Answers9

42

Try This one It will Run perfectly..

[[UIApplication sharedApplication] setStatusBarHidden:YES];

And in XIB set none option for the status bar.

for iOS 7.

Go to info.plist and add two attributes if not present. set "Status bar is initially hidden" to "YES" and set "UIViewControllerBasedStatusBarAppearance" to "NO". This will hide status bar for your app.

Kartik
  • 779
  • 7
  • 22
  • i think in your case the bit about setting the XIB to have no status bar and to make sure the size of the view covers where the status bar would have been are the things you need to do. If your XIB has the status bar and then you hide it within the code the view is not necessarily going to expand to cover that now empty space. – carbonbasednerd Nov 30 '11 at 13:59
  • setting the xib setting did the trick!!! I only set the statusbar hidden in the init method.. not the xib :P – jsetting32 Jul 01 '13 at 16:51
17
#pragma mark - Hide statusbar

-(void)hideStatusBar {
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
    // iOS 7.0 or later
    [self setNeedsStatusBarAppearanceUpdate];
#else
    // less than 7
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
#endif
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}
neoneye
  • 50,398
  • 25
  • 166
  • 151
8

If there is anyone looking for a solution where the above solution does not work (and there is still an annoying blue 20px gap at the top), try putting this in the viewWillAppear on the implementation file of the view controller that you would like the status bar to be hidden.

self.navigationController.navigationBar.frame = CGRectOffset(self.navigationController.navigationBar.frame, 0.0, -20.0);

That literally took me 12 hours or so to fix, and that was the solution, so now i'm spreading the word in case anyone else has this annoying issue.

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
  • That solution worked for me but did not give smooth view traditions. This worked better for me: – T.J. Jan 23 '12 at 20:49
  • [UIView animateWithDuration:0.35f delay:0.0f options:UIViewAnimationOptionOverrideInheritedCurve animations:^{ self.navigationController.navigationBar.frame = CGRectOffset(self.navigationController.navigationBar.frame, 0.0, 20.0); } completion:nil]; – T.J. Jan 24 '12 at 02:54
  • 1
    I have just looked at this a year or so on with a bit more experience under my belt, and this isn't a great solution although it works. Just a warning. I'm uncorrecting my answer and marking Kartik's correct. – Adam Waite Mar 27 '13 at 09:40
  • @T.J., that works great, but with a negative sign in front of the 20.0, viz., `[UIView animateWithDuration:0.35f delay:0.0f options:UIViewAnimationOptionOverrideInheritedCurve animations:^{ self.navigationController.navigationBar.frame = CGRectOffset(self.navigationController.navigationBar.frame, 0.0, -20.0); } completion:nil];`. – JohnK Jul 04 '13 at 00:37
7

Kartik's solution worked for me.

[[UIApplication sharedApplication] setStatusBarHidden:YES];

I added this to viewWillAppear: instance method.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    self.webView.scalesPageToFit = YES;
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.co.uk"]]];
}

And I spent ages on this too. Using Xcode 4.2, iOS5 sim.

But when I first implemented it, the annoying "space" at the top was there. I selected the View Controller in the storyboard and set the properties as follows: Size: Full Screen StatusBar: None everything else inferred.

I checked wants full screen.

Voila, it all worked fine.

Stunner
  • 12,025
  • 12
  • 86
  • 145
WillShakes
  • 71
  • 1
  • 1
  • Bear in mind that while Adam's solution is nice, it is like sweeping dust under the rug. You want full screen, you want the status bar not to be there, not just hidden and then the space covered up. – WillShakes Feb 02 '12 at 11:03
2

I know this is an old question but none of this answers works for me, so this is how it works for me to hide the status bar in a single viewController

First in your parentViewController you have to set:

- (UIViewController *)childViewControllerForStatusBarHidden {
    if ( hideStatusBarViewController ) {
        return hideStatusBarViewController;
    }
    return nil
}

It only returns the child view controller when its created otherwise nil is the default. When you add your hideStatusBarViewController you must call

[self setNeedsStatusBarAppearanceUpdate];

on the parentViewController, this function forces to read childViewControllerForStatusBarHidden. Finally in hideStatusBarViewController you must set

- (BOOL)prefersStatusBarHidden {
    return YES;
}

Its the only solution that works for me. I hope it help somebody.

Patricio Bravo
  • 406
  • 6
  • 8
1

This is solution if you want to hide status bar on a single view

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [self.view sizeToFit];
}
Sihad Begovic
  • 1,907
  • 1
  • 31
  • 33
1

I would suggest you a different approach: insert that view onto the application's window:

YourUIAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate.window insertSubview:self.yourView atIndex:([[appDelegate.window subviews]count])];

That way it will show over the status bar

I hope it helps you

Fran Sevillano
  • 8,103
  • 4
  • 31
  • 45
0

Here is a snippet of code that might help. When the view loads show the status bar, when you leave the view hide it again.

-(void)viewWillAppear:(BOOL)animated {
    if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    else 
        [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
}


-(void)viewWillDisappear:(BOOL)animated  {
    if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    else 
        [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}
Louie
  • 5,920
  • 5
  • 31
  • 45
  • Nope, doesn't help unfortunately. Thanks anyway. – Adam Waite Nov 30 '11 at 15:46
  • Just FYI - the above code is super wrong. It should be more like: `(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade]; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade]; }` – snibbe Sep 13 '14 at 03:53
-1

The [[UIApplication sharedApplication] setStatusBarHidden:BOOL]; is enough, but:

  1. Remember to set it back to NO before leaving the view or the page you return to will likely have the Nav Bar under the status bar.

  2. Make sure that you set both status bar hide and show in the view in which you want the status bar hidden. I did the switch off in the viewDidLoad method, and - crucially - the switch back on in the viewWillDisappear:animated method. Any later and you're in for trouble.

Danilo
  • 3,257
  • 2
  • 19
  • 24
David
  • 1,018
  • 13
  • 22