Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to present navigation bar for UIImagePickerController, source type UIImagePickerControllerSourceTypeCamera, in iOS 7?

In iOS 6, I was using the following code to push a UIImagePickerController, of source type UIImagePickerControllerSourceTypeCamera, and to show its navigation bar. I wanted to show the navigation bar because after taking the image, I'm pushing another VC that allows the user to set some attributes in the database.

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    cameraController = [[UIImagePickerController alloc] init];

    cameraController.delegate = self;
    cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:cameraController animated:YES completion:NULL];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    cameraController.topViewController.title = @"Add";
    cameraController.navigationBar.translucent = NO;
    cameraController.navigationBar.barStyle = UIBarStyleDefault;

    [cameraController setNavigationBarHidden:NO animated:NO];
}

In iOS 7 this code no longer shows the navigation bar. Does anyone know if there's a way to to get the navigation bar back for UIImagePickerController, of source type UIImagePickerControllerSourceTypeCamera?

like image 713
Jeff V Avatar asked Oct 09 '13 15:10

Jeff V


2 Answers

Guess what? When imagePicker presents, it's automatic set to hidden....
All you need to do is setHidden:NO in next runloop. Like:

[self presentModalViewController:imagePicker animated:YES];
[self performSelector:@selector(showNavigationBar:) withObject:imagePicker afterDelay:0];

- (void)showNavigationBar:(UIImagePickerController*)imagePicker {
    [imagePicker setNavigationBarHidden:NO];
}
like image 152
LeverkusenFan Avatar answered Sep 23 '22 05:09

LeverkusenFan


@LeverkusenFan's solution works well. But instead of using a hack such as a run loop, you use the completion handler of presentViewController to achieve that effect.

[self presentViewController:cameraController animated:YES completion:^{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    cameraController.topViewController.title = @"Add";
    cameraController.navigationBar.translucent = NO;
    cameraController.navigationBar.barStyle = UIBarStyleDefault;

    [cameraController setNavigationBarHidden:NO animated:NO];
}];

In fact a better solution that avoids the weird animation when the navigation bar shows up and which works well when you press the back button on the nav bar is as follows:

In the delegate for the UIImagePickerController implement the following function.

- (void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (navigationController == self.cameraController && navigationController.viewControllers.count == 1) {
        // When showing the ImagePicker update the status bar and nav bar properties.
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

        navigationController.topViewController.title = self.cameraTitle;
        navigationController.navigationBar.translucent = NO;
        navigationController.navigationBar.barStyle = UIBarStyleDefault;
        [navigationController setNavigationBarHidden:NO animated:animated];
    }
}

This function will get called when the ImagePicker is shown and we only make the changes for the rootViewController of the ImagePicker (i.e. the camera screen).

like image 33
Kostub Deshmukh Avatar answered Sep 25 '22 05:09

Kostub Deshmukh