Presenting a Modal View Controller hides the Navigation Bar - objective-c

I have a navigation based app with a navigation bar, but there are a few instances where instead of pushing a view controller onto the stack, I need to present the view controller modally. The problem is that when I dismiss the modal view controller, everything functions as expected except that the navigation bar is hidden and the (parent view) has been resized, which is the expected behavior according to the docs. So I figured I could simply call a built-in method to unhide the navigation bar. I have already tried
[self.navigationController setNavigationBarHidden:NO];
as well as the animated version without success.
The documentation talks about this in the method
presentModalViewController: animated:
in the discussion section where it says,
On iPhone and iPod touch devices, the view of modalViewController is always presented full screen" and "Sets the modalViewController property to the specified view controller. Resizes its view and attaches it to the view hierarchy."However, the docs didn't clue me in as to how to undo this process after dismissing a modal view.
Has anyone else experienced this and found a solution?
Edit: I am having this same problem, so instead of asking my own question I am sponsoring a bounty on this one. This is my specific situation:
In my case, I am presenting an Image Picker in a Modal View Controller, over a Navigation Controller:
-(void) chooseImage {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
imagepicker = [[UIImagePickerController alloc] init];
imagepicker.allowsEditing = NO;
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagepicker.navigationBar.opaque = true;
imagepicker.wantsFullScreenLayout = NO;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (self.view.window != nil) {
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagepicker];
[popoverController presentPopoverFromBarButtonItem:reset permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
} else {}
} else {
[self.navigationController presentModalViewController:imagepicker animated:YES];
}
}
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[self.popoverController dismissPopoverAnimated:true];
} else {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
//Save the image
}
-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[self.popoverController dismissPopoverAnimated:true];
} else {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
}

Make sure you a presenting AND dismissing the modalViewController from the UINavigationController, like so:
// show
[self.navigationController presentModalViewController:vc animated:YES];
// dismiss
[self.navigationController dismissModalViewControllerAnimated:YES];
If your view controller is actually on the UINavigationController's stack then this is the correct way to handle the presentation and dismissal of the modal view controller. If your UINavigationBar is still hidden, there is something else funky going on and we would need to see your code to determine what is happening.
Edit
I copied your code into an app of mine and the UIImagePickerController successfully presented and dismissed and my UINavigationController's UINavigationBar was still there. I truly believe that the problem lays elsewhere in your architecture. If you upload a zip w/ an example project I will take a look.

Simply try following code it will work
SettingsViewController *settings = [[SettingsViewController alloc] init];
UINavigationController *navcont = [[UINavigationController alloc] initWithRootViewController:settings];
[self presentModalViewController:navcont animated:YES];
[settings release];
[navcont release];
One need to present the navigation controller in order to have navigation bar on the presented controller

I think I've seen this behavior when presenting a view controller on the wrong VC. Are you calling presentModalViewController on the navigation controller or the individual VC?
Try calling it from the navigationController if you aren't already.
[self.navigationController presentModalViewController:myVC animated:YES];

If you present a controller as model, View controller will appear to total view.
If you want to access the navigation controller properties over the model view, You need to create another navigation controller reference and it continues as previous.
This may be useful for you.

Check this out. This is Apple's Documentation under UIViewController Class Reference:
It clearly mentions that modal view always presents in full screen mode, so it is obvious that navigation bar will be hidden. So put the seperate navigation bar on modal view to navigate back.
presentModalViewController:animated:
Presents a modal view managed by the given view controller to the user.
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
Parameters
modalViewController
The view controller that manages the modal view.
animated
If YES, animates the view as it’s presented; otherwise, does not.
Discussion
On iPhone and iPod touch devices, the view of modalViewController is always presented full screen. On iPad, the presentation depends on the value in the modalPresentationStyle property.
Sets the modalViewController property to the specified view controller. Resizes its view and attaches it to the view hierarchy. The view is animated according to the transition style specified in the modalTransitionStyle property of the controller in the modalViewController parameter.
Availability
Available in iOS 2.0 and later.
Hope this helps you understand that hiding the whole view along with navigation controller is default behaviour for modal view so try putting a seperate navigation bar in modal view to navigate.
You can check it further on this link
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

AddContactVC *addController =[self.storyboard instantiateViewControllerWithIdentifier:#"AddContactVC"];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:addController];
[self presentViewController:navigationController animated:YES completion: nil];
working for me shows navigation bar

Emphatic and Devin –
As I started reading through the Apple docs to get familiar with the problem, I noticed that the method you're using, presentModalViewController:animated:, appears to be deprecated in favor of presentViewController:animated:completion:. Perhaps you should try to use that method instead.
For your convenience, take a look for yourself:
presentModalViewController:animated: reference
I'll try to put together a quick test program to see whether what I've said above is actually true. But give it a shot – maybe it'll help!

Xcode has a template that does pretty close to what you're doing. from the results, i don't think you should be attempting to perform [self.navigationController presentModalViewController:vc] and [self.navigationController dismissModalViewControllerAnimated:] , but rather simply [self presentModalViewController:] and [self dismissModalViewControllerAnimated:] .
to see how the template does this for yourself, you can use the new project wizard in xcode 4.3 . perhaps it will provide some guidance:
from that choice, choose Next, then give your test project a name, choose "Universal", turn off automatic reference counting, hit next, save where you want it.
now, click on the target and switch the deployment target to 4.3 (or 4.0 if you prefer) for your testing purposes, and switch to your device or the iOS 4.3 simulator .
finally, substitute the following code in applicationDidFinishLaunching:withOptions: in the created AppDelegate.m:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.mainViewController = [[[MainViewController alloc] initWithNibName:#"MainViewController_iPhone"
bundle:nil] autorelease];
} else {
self.mainViewController = [[[MainViewController alloc] initWithNibName:#"MainViewController_iPad"
bundle:nil] autorelease];
}
UINavigationController* navigationController
= [[UINavigationController alloc] initWithRootViewController:self.mainViewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
now, when i run this, it doesn't hide the navigationBar. and in the created MainViewController.m from the template, you'll see how it presents the modal view controller and dismisses it from the controller itself and not from the navigation controller. for good measure, to make the template code more like your own, go into MainViewController.m and delete the line that sets the modal view controller transition style ...
(of course, in iOS 5, with storyboards, the same thing can all be accomplished with modal segues ... which is how i've done this for apps that i'm not supporting for pre-5.0 that present a modalViewController in this fashion.)

One of the best solution it to use this Category MaryPopin
https://github.com/Backelite/MaryPopin

Related

modal segue to a xib from a xib?

I have an app that uses only xibs, no storyboards.
I have created a prompt xib that I would like to present modally (with the modal animation) from a table view controller xib (named TVC.xib) The TVC is nested in a navigation controller.
I can get the prompt to present itself, but I want it to present itself with a modal animation. Unfortunately, the presentModalViewController has been deprecated. What is the current option to present a view controller modally in code and have it animate the same way that modal presentations used to animate?
Here is my code: (in the TVC.m)
PromptViewController *promptVC = [[PromptViewController alloc] initWithNibName:#"PromptXib" bundle:nil];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:promptVC];
[self.navigationController presentViewController:navVC animated:YES completion:^{
NSLog(#"presented prompt vc");
}];
Ideally I could replace the method in the 3rd line with self.navigationController presentMODALViewController... etc, but it's deprecated.
You are looking for:
[self presentViewController:aViewController animated:animated completion:^{}];
But you should browse some tutorials to update the knowledge.
I've figured it out. I needed to set the transition style and presentation style on the view controller that I wanted to show. Here is my solution:
PromptViewController *promptVC = [[PromptViewController alloc] initWithNibName:#"PromptXib" bundle:nil];
promptVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
promptVC.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:promptVC animated:YES completion:nil];

Switching between storyboards/view controllers

So I'm making an iPad app for the very first time and right now I have one view controller with its buttons displayed on the storyboard and everything and what I want to do is when I click on a certain button, it brings me to a new screen.
So I created a second viewcontroller class for the second screen and I created an IB Action method for the button but it's empty because I don't know how to implement it. So what do I have to do to accomplish this?
Try it....
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// iPad-specific interface here
LBAlertLoginViewController *lbAlertLoginVc = [[LBAlertLoginViewController alloc]initWithNibName:#"LBAlertLoginViewController_iPad" bundle:nil];
[self.navigationController pushViewController:lbAlertLoginVc animated:YES];
[lbAlertLoginVc release];
}
else
{
// iPhone and iPod touch interface here
LBAlertLoginViewController *lbAlertLoginVc = [[LBAlertLoginViewController alloc]initWithNibName:#"LBAlertLoginViewController_iPhone" bundle:nil];
[self.navigationController pushViewController:lbAlertLoginVc animated:YES];
[lbAlertLoginVc release];
}
Hope i helped.

Going back to first ViewController from second ViewController

I'm building an app that currently has 3 ViewControllers. One of them is used after a successful login so is not relevant in this question.
I'm using a mixture of Storyboards and building things programmatically when I find Storyboards do not give me the fine control that I need.
The first ViewController is built in my 'MainStoryboard'. It has a login form and an info button at the bottom. I link it up the my AppDelegate by doing the following inside didFinishLaunchingWithOptions:
ViewController *viewController = (ViewController *)self.window.rootViewController;
Because I wanted to force rendering of a UIWebView (another story) I create the second view programmatically. I do the following inside didFinishLaunchingWithOptions:
infoViewController = [[InfoViewController alloc] init];
[infoViewController view];
Inside both of my ViewControllers I setup a link to appDelegate as below:
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
I have an info button in my first ViewController that takes you to the infoViewController. It calls the following code when tapped:
appDelegate.infoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:appDelegate.infoViewController animated:YES];
The above works just fine for me, flips over the screen and shows the InfoViewController.
On my InfoViewController I have a button that should take you back to the login page, I have tried all sorts to get this to work but it just crashes my app. Nothing seems to work. I have tried the following:
appDelegate.viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:appDelegate.viewController animated:YES];
and
[self.navigationController popToRootViewControllerAnimated:YES];
and
[self.navigationController popViewControllerAnimated:YES];
and
[self.navigationController popToViewController:appDelegate.viewController animated:YES];
I suspect the last 3 might be more to do with when you have a navigation view controller and you want to go back to the root? I'm not sure, but either way it does not work. I had this working using storyboards previously so I'm sure it ought to be easy! As mentioned I switched to making the infoViewController programmatically so that I could force the UIWebView to render before the view appeared.
Any help much appreciated.
You can do with:
[self dismissModalViewControllerAnimated:YES];
You should use this.
[self dismissModalViewControllerAnimated:YES];
You should use a main controller for switching between your other view controllers. Change the view of your root controller to one of your other view controllers (apply animations as usual if needed). Hold a pointer to your root controller in your other view controllers and call self.rootController.view = <desired_controller_instance>.view
I think the way you're presenting your InfoViewController is wrong. Do it the following way:
In your ViewController, create an action for the info button.:
- (IBAction)infoButtonTapped:(id)sender
{
InfoViewController *infoViewController = [[InfoViewController alloc] init];
infoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:infoViewController animated:YES];
}
And in your InfoViewController, in the action of your button that should take you back write this:
- (void)takeBackToViewController
{
[self dismissModalViewControllerAnimated:YES];
}
Hope it works.
Also in presented controller you can use this
if(self.parentViewController)
[self.parentViewController dismissModalViewControllerAnimated:YES];
else
[self.presentingViewController dismissModalViewControllerAnimated:YES];
To dissmiss current controller.

Present UIPopoverController from inside a modally presented view controller

Is it impossible to present a popover controller from a view controller that's presented as UIModalPresentationFormsheet? or am I missing something?
This code works fine on a non-modal view controller, and displays the popover correctly.(WEPopoverController is a custom implementation from here.)
GenericDataTableViewController *genericDataController = [[GenericDataTableViewController alloc] initWithNibName:#"GenericDataTableViewController" bundle:[NSBundle mainBundle]];
genericDataController.dataSource = [NSMutableArray arrayWithObjects:#"asli", nil];
genericDataController.delegate = self;
genericDataController.contentSizeForViewInPopover = CGSizeMake(300, 46 * [genericDataController.dataSource count]);
self.popoverController = [[[WEPopoverController alloc] initWithContentViewController:genericDataController] autorelease];
self.popoverController.delegate = self;
[self.popoverController presentPopoverFromRect:((UIButton *)sender).frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
[genericDataController release];
But when I put this inside the modal view controller, this presents the popover below the modal controller, i.e. on the view controller that presents the modal one. So it cannot be seen by the user.
How can I solve this?
The answer is explained in the comments of the question but I'm summarizing it here for a cleaner Q&A.
There aren't any restrictions about displaying a popover controller from inside a form sheet, if your popover controller is UIPopoverController. My problem was due to the implementation of WEPopoverController.
So, sacrifice the visual experience and continue with the good old UIPopoverController.

Create a UINavigationBar without using [Projectname]AppDelegate?

I'm trying to create another UINavigationBar in my project, but it seems that I'm missing some key detail. When the application first loads, it does have it's own navigation system, but now I'm trying to add another navigation to a modal.
Many tutorials show you need to connect the view to the [self window], which only seems to work in the AppDelegate files, but when I've tried placing the code* in viewDidLoad, I can never seem to build without any errors.
I've seen this in multiple apps, but how is this done (programmatically or with IBuilder)?
Thanks!
Example code I've tried in viewDidLoad
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[self viewController]];
[self.window addSubview:navigationController.view];
You don't show enough code to be able to understand how you do it, but it seems to me that you are showing a controller modally, then trying to add as a subview to its view a navigation controller.
You can try and directly push modally your navigation controller (from your app delegate or where it makes sense for your app):
(IBAction) navigateToSecondaryViewController {
if (secondaryViewController == nil) {
informationTableViewController = [[SecondaryViewController alloc]
initWithNibName:#"SecondaryViewController"
bundle:[NSBundle mainBundle]];
secondaryViewController.delegate = self;
}
if (navController == nil) {
navController = [[UINavigationController alloc]
initWithRootViewController:secondaryViewController];
}
[self presentModalViewController:navController animated:YES];
}
Full example here.