Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presenting a Modal View Controller hides the Navigation Bar

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];
    }
}
like image 418
EmphaticArmPump Avatar asked Aug 11 '11 23:08

EmphaticArmPump


3 Answers

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.

like image 78
Michael Frederick Avatar answered Nov 07 '22 14:11

Michael Frederick


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

like image 27
Priyank Ranka Avatar answered Nov 07 '22 15:11

Priyank Ranka


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];
like image 3
Bob Spryn Avatar answered Nov 07 '22 14:11

Bob Spryn