Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presenting modal view occasionally hides the navigation bar

I've come across this twice now.

Sometimes using the following line of code:

[self.navigationController presentModalViewController:aViewController animated:YES];

displays the view, but the navigation bar is then hidden.

I can write:

[self.navigationController setNavigationBarHidden:NO];

to my hearts content, everywhere I can think of with no effect.

Has anyone ran into this?

Am I doing something silly?

like image 918
Corey Floyd Avatar asked Jun 04 '09 00:06

Corey Floyd


4 Answers

No, I ran into this as well. The problem is that when you present a modal view controller with a UIViewController based class, it does not extend the calling navigation controller's nav bar onto the modal. The modal view covers the entire screen. What I ended up doing to solve the problem was to create a UINavigationController and push the UIViewController based class onto it, and then do presentModalViewController to the navigation controller's instance.

like:

UIViewController *vc = [[UIViewController alloc] init];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentModalViewController:cntrol animated:YES];
[cntrol release];

That allowed me to have a nav bar at the top.

I am not sure if that will help in your particular case, the only other thing I would suggest is to replicate the behavior of the modal with a UIAnimation that stops 44px below the top of the phone. That would keep the original navigation bar visible.

like image 62
Heat Miser Avatar answered Oct 23 '22 21:10

Heat Miser


@HeatMiser shows a great way to get around the "bug" surrounding the inability to display items on the nav bar. I'm not sure, however, if this is strictly a bug in Presentation, since modal operations ought to trump the underlying view's interface theme. Having the modal operation's theme mimic the underlying UI theme is fine, but wrapping the true modal view with a navigation view feels wrong to me (extra view object just to get a little more behavior).

Instead, the following worked for me and gives the same behavior as "New Message" does in the Mail program (on the iPhone).

In IB, place a UIToolBar at the top of the modal screen (mimicking the navigation bar) with "Cancel" and "Save" UIBarButtonItem's and a Flexible Space Bar Button Item in between to get the buttons to align left and right. Then, add a UILabel centered over the UIToolBar (The Font Helvetica, Bold, Size 18 appears to match the Navigation Bar Title). Connect the buttons to IBAction's on the modal's UIViewController, and you're done.

like image 41
Andrew Philips Avatar answered Oct 23 '22 20:10

Andrew Philips


If there is a navigation controller active, then you should just use

[self.navigationController pushViewControllerAnimated:how];

to slide another view controller in, while giving yourself and the user into a consistent user interface complete with 'automatic' back button support.

Once a navigation controller is in use, presenting a modal view controller should only be done to enlarge the usable area on the screen. And then, you should really use a fancy animation to let the user know that you are stepping away from the "task" or "steps" that the navigation controller was embodying.

like image 32
grwww Avatar answered Oct 23 '22 20:10

grwww


Maybe this is obvious, but once you're done with the modal view and want to dismiss it, you should do something like this in your modal vc:

[parentController dismissModalViewControllerAnimated:YES];

Where parentController is a reference to the vc from where you are presenting the modal view.

like image 41
Fede Mika Avatar answered Oct 23 '22 19:10

Fede Mika