tags:

views:

515

answers:

2

Hi,

I have a UINavigationController with a visible Navigation Bar. I have one particular UIViewController which I'd like to hide the status bar when pushed into the navigation stack. Once this viewController is popped I'd like to show the status bar again.

I'm hiding the bar in the viewWillAppear method of my UIViewController like this:

- (void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    [self.navigationController setWantsFullScreenLayout:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];    
}

Note that I'm setting setWantsFullScreenLayout:YES here for clarity, but I'm actually just setting this property in Interface Builder.

The problem: The navigation bar of the NavigationController doesn't move up to take the space of the now hidden status bar.

A hacky solution The only thing I found that worked to refresh the position of the nav bar was to hide it and show it again, like this:

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.navigationController setNavigationBarHidden:NO animated:NO];

but this is clearly a hack, there has got to be a better way.

Other things I tried:

  1. I tried calling the [super viewWillAppear] after hiding the status bar, i.e. at the end of my method.

  2. I tried setNeedsLayout on the navigationController.view like this:

    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
    [self.navigationController.view setNeedsLayout];

but that doesn't seem to work.

Any help appreciated. Thanks

A: 

In your root view controller (where you want to show the status bar):

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

    [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];;
}

In the view controller you push on the stack (where you want to hide the status bar):

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

    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];;
}

Edit:

I realize now you want to hide the status bar. Got them mixed up since you were showing/hiding the nav bar in the code you posted. My mistake. It's essentially the same code, anyway:

In your root view controller (where you want to show the status bar):

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

    [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
}

In the view controller you push on the stack (where you want to hide the status bar):

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

    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}

I just tested this with an existing project and it worked.

Shaggy Frog
What I'm trying to hide is the status bar, not the nav bar. But I'd like the nav bar to move up when the status bar is hidden.Thanks.
eddy
Fixed above. :)
Shaggy Frog
Thanks Shaggy, the problem is that the nav bar is not updating it's position to take the space the status bar left when hidden. How do I get the nav bar to move up to fill the status bar space?
eddy
The nav bar shifts up for me. Did you try the above code, and did you remove all your other bits and pieces of code?
Shaggy Frog
yes, exactly the same code, all the rest removed. My nav bar doesn't move up.I'm using 3.1.1, are you trying this on 3.1.2?
eddy
I use 3.1.2, yes, but I don't think that will make a difference versus using 3.1.1
Shaggy Frog
A: 

You should try resizing the frame of your UIViewControllers's view after you have hidden the StatusBar. The applicationFrame updates it's origin.y and size.height during the setStatusBarHidden:animated: method.

CGRect rect = [UIScreen mainScreen].applicationFrame;
self.view.frame = rect;

[self.view setNeedsLayout];
ianegordon