Adam Waite
Adam Waite

Reputation: 18845

Hide the status bar on iPhone on a single view?

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.

Upvotes: 32

Views: 43527

Answers (9)

Patricio Bravo
Patricio Bravo

Reputation: 416

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.

Upvotes: 2

Kartik
Kartik

Reputation: 779

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.

Upvotes: 42

neoneye
neoneye

Reputation: 52161

#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;
}

Upvotes: 17

WillShakes
WillShakes

Reputation: 71

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.

Upvotes: 7

David
David

Reputation: 1050

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.

Upvotes: -1

Sihad Begovic
Sihad Begovic

Reputation: 1947

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];
}

Upvotes: 1

Adam Waite
Adam Waite

Reputation: 18845

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.

Upvotes: 8

Louie
Louie

Reputation: 5940

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];
}

Upvotes: 0

Fran Sevillano
Fran Sevillano

Reputation: 8163

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

Upvotes: 1

Related Questions