关闭 UIImagePickerController 会将 UITabBarController 重置为 0 索引 - iOS

Dismissing UIImagePickerController resets UITabBarController to 0 index - iOS

我的 UITabBarController 中有 4 个选项卡,我在我的 tabBar 中显示第 4 个 ViewController 中的 UIImagePickerController。 (所有选项卡 viewController 都嵌入在 UINavigationController 中)

当我的 UIImagePickerController 从我的第 4 个选项卡中退出时,无论是在选择图像之后还是只是取消选择器后,它都会将我带回到我的第一个选项卡(0 索引)。

它应该留在我的第 4 个选项卡 viewController。为什么会这样?我已经搜索了所有代码,没有我提到的地方 tabBarController.setSelectedIndex = 0;

这是我的代码,用于显示 UIImagePickerController 并关闭它:

- (void)takeNewPhotoFromCamera
{
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        controller.sourceType = UIImagePickerControllerSourceTypeCamera;
        controller.allowsEditing = NO;
        //controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
        controller.delegate = self;
        [self presentViewController: controller animated: YES completion: nil];

    }
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];  
}

请帮忙。

谢谢!

刚刚通过将图像选择器的 modalPresentationStyle 设置为 OverCurrentContext 找到了解决方案:

UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
controller.allowsEditing = NO;
controller.delegate = self;
controller.modalPresentationStyle = UIModalPresentationOverCurrentContext; //This is the magical line
[self presentViewController: controller animated: YES completion: nil];