在iOS 7中以任何方式呈现UIImagePickerController的导航栏,源类型为UIImagePickerControllerSourceTypeCamera?

Jef*_*f V 5 uiimagepickercontroller uinavigationcontroller ios

在iOS 6中,我使用以下代码来推送UIImagePickerController源类型UIImagePickerControllerSourceTypeCamera,并显示其导航栏.我想显示导航栏,因为在拍摄图像后,我正在推送另一个VC,允许用户在数据库中设置一些属性.

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];
}
Run Code Online (Sandbox Code Playgroud)

在iOS 7中,此代码不再显示导航栏.有没有人知道是否有办法获取UIImagePickerController源类型的导航栏UIImagePickerControllerSourceTypeCamera

小智 5

你猜怎么着?当imagePicker出现时,它会自动设置为隐藏....
你需要做的就是setHidden:下一个runloop中的NO.喜欢:

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

- (void)showNavigationBar:(UIImagePickerController*)imagePicker {
    [imagePicker setNavigationBarHidden:NO];
}
Run Code Online (Sandbox Code Playgroud)


Kos*_*ukh 3

@LeverkusenFan 的解决方案效果很好。但是,您可以使用presentViewController 的完成处理程序来实现该效果,而不是使用运行循环之类的技巧。

[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];
}];
Run Code Online (Sandbox Code Playgroud)

事实上,一个更好的解决方案可以避免导航栏显示时出现奇怪的动画,并且当您按下导航栏上的后退按钮时效果很好,如下所示:

在 UIImagePickerController 的委托中实现以下函数。

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

当显示 ImagePicker 并且我们只对 ImagePicker 的 rootViewController(即相机屏幕)进行更改时,将调用此函数。