5

在 iOS 6 中,我使用以下代码来推送UIImagePickerControllersource 类型的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];
}

在 iOS 7 中,此代码不再显示导航栏。有谁知道是否有办法让导航栏恢复UIImagePickerController源类型UIImagePickerControllerSourceTypeCamera

4

2 回答 2

5

你猜怎么了?当 imagePicker 出现时,它会自动设置为 hidden....
你需要做的就是在下一个 runloop 中 setHidden:NO 。像:

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

- (void)showNavigationBar:(UIImagePickerController*)imagePicker {
    [imagePicker setNavigationBarHidden:NO];
}
于 2013-11-28T11:18:07.907 回答
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];
}];

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

在 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(即相机屏幕)进行更改。

于 2015-06-27T01:56:04.083 回答