Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

presentViewController and displaying navigation bar

I have a view controller hierarchy and the top-most controller is displayed as a modal and would like to know how to display the navigation bar when using

'UIViewController:presentViewController:viewControllerToPresent:animated:completion' 

The docs for 'presentViewController:animated:completion:' note:

'On iPhone and iPod touch, the presented view is always full screen. On iPad, the presentation depends on the value in the modalPresentationStyle property.'

For 'modalPresentationStyle', the docs say:

The presentation style determines how a modally presented view controller is displayed onscreen. On iPhone and iPod touch, modal view controllers are always presented full-screen, but on iPad there are several different presentation options.

Is there way to ensure that the navigation bar is visible below the status bar once the view control displays itself? Should I interpret the doc as, you don't get any options of iPhone/iPod and only on iPad?

Previously, I was using 'UIViewController:presentModalViewController:animated' which worked fine, but since iOS 5.0, the API has been deprecated so I'm switching over to the new one.

Visually, what I'm looking to do is have the new controller slide in from the bottom of the screen, just like the old API used to do.

[updating with code]:

// My root level view: UIViewController *vc = [[RootViewController alloc]                              initWithNibName:nil                              bundle:[NSBundle mainBundle]]; navController = [[UINavigationController alloc] initWithRootViewController:vc];         ....  // Within the RootViewController, Second view controller is created and added  // to the hierarchy. It is this view controller that is responsible for  // displaying the DetailView: SecondTierViewController *t2controller = [[SecondTierViewController alloc]                                             initWithNibName:nil                                            bundle:[NSBundle mainBundle]];  [self.navigationController pushViewController:t2controller animated:YES];  // Created by SecondTierViewController  DetailViewController *controller = [[DetailViewController alloc] initWithNibName:nil                                                                                                                          bundle:[NSBundle mainBundle]];    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; controller.modalPresentationStyle = UIModalPresentationCurrentContext;  [self.navigationController presentViewController:controller                                          animated:YES                                          completion:nil]; 
like image 930
Jonas Gardner Avatar asked Mar 15 '12 16:03

Jonas Gardner


People also ask

How do you add navigation to a storyboard?

Go to the Storyboard. Select the View Controller and in The Editor menu select Embed in -> Navigation Controller. Next, drag a Bar Button from the Object Library to the left side of the Navigation Bar and name it "Left Item". Repeat this for the right side and name it "Right Item".


2 Answers

It is true that if you present a view controller modally on the iPhone, it will always be presented full screen no matter how you present it on the top view controller of a navigation controller or any other way around. But you can always show the navigation bar with the following workaround way:

Rather than presenting that view controller modally present a navigation controller modally with its root view controller set as the view controller you want:

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:nil bundle:nil]; UINavigationController *navigationController =      [[UINavigationController alloc] initWithRootViewController:myViewController];  //now present this navigation controller modally  [self presentViewController:navigationController                    animated:YES                    completion:^{                          }]; 

You should see a navigation bar when your view is presented modally.

like image 104
Manish Ahuja Avatar answered Nov 15 '22 22:11

Manish Ahuja


Swift 5.*

Navigation:

guard let myVC = self.storyboard?.instantiateViewController(withIdentifier: "MyViewController") else { return } let navController = UINavigationController(rootViewController: myVC)  self.navigationController?.present(navController, animated: true, completion: nil) 

Going Back:

self.dismiss(animated: true, completion: nil) 

Swift 2.0

Navigation:

let myVC = self.storyboard?.instantiateViewControllerWithIdentifier("MyViewController"); let navController = UINavigationController(rootViewController: myVC!)  self.navigationController?.presentViewController(navController, animated: true, completion: nil) 

Going Back:

self.dismissViewControllerAnimated(true, completion: nil) 
like image 44
Tal Zion Avatar answered Nov 16 '22 00:11

Tal Zion