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

时间:2013-10-09 15:57:38

标签: ios uinavigationcontroller uiimagepickercontroller

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

UIImagePickerControllerSourceTypeCamera

在iOS 7中,此代码不再显示导航栏。有没有人知道是否有办法让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]; } 的源类型为UIImagePickerController的导航栏?

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:3)

@LeverkusenFan的解决方案效果很好。但是,不使用诸如运行循环之类的hack,而是使用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];
}];

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

在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];
    }
}

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