关于ios:关闭UIImagePickerController也关闭呈现视图控制器

Dismissing of UIImagePickerController dismisses presenting view controller also

我正在一个使用标签栏系统的项目中。标签栏的一项是JobPostingViewController。我将其嵌入到UINavigationController中。在该视图控制器中有一个名为添加新作业的UIButton。我实现了pushviewcontroller来执行CreateJobPostViewController。在那里,我需要添加UIImagePickerController来选择图像。当我点击"完成"按钮或从库中选择图像时,它将解散到JobPostingViewController。但它应该转到CreateJobPostViewController。
任何人请帮助我。预先感谢。

您可以在此处看到问题:
enter

1
2
3
4
 @IBAction func openCreateJob(sender: AnyObject) {
    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("CreateJobPostViewController") as! CreateJobPostViewController
    self.navigationController?.pushViewController(vc, animated: true)
}

CreateJobPostViewController

中的代码

1
2
3
4
5
6
7
8
9
10
11
   @IBAction func addImages(sender: AnyObject) {
    imagePicker.allowsEditing = false
    imagePicker.sourceType = .PhotoLibrary
    presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    picker.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    picker.dismissViewControllerAnimated(true, completion: nil)
}


通过将图像选择器的modalPresentationStyle设置为" OverCurrentContext"来解决此问题:

1
picker.modalPresentationStyle = .overCurrentContext


将选择器添加为子视图

尝试将图像选择器作为子视图添加到创建它的CreateJobPostViewController中,然后将其从代理中的父项中移除

1
2
3
4
5
6
7
8
9
10
@IBAction func openCreateJob(sender: AnyObject) {

var picker: UIImagePickerController = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = false
picker.sourceType = .PhotoLibrary
self.addChildViewController(picker)
picker.didMoveToParentViewController(self)
self.view!.addSubview(picker.view!)
}

然后

1
2
3
4
5
 func imagePickerControllerDidCancel(picker: UIImagePickerController) {

    picker.view!.removeFromSuperview()
    picker.removeFromParentViewController()
    }

用于演示

在当前上下文中显示选择器,并带有诸如"编辑取消选择"之类的选项,

使用picker.modalPresentationStyle = .overCurrentContext //在展示之前

1
 presentViewController(picker, animated: true, completion: nil)


您可以使用:

1
picker.modalPresentationStyle = .overCurrentContext

,但根据您的屏幕布局,最好使用:

1
picker.modalPresentationStyle = .overFullScreen

否则,您的"取消","重新拍摄"和"使用照片"按钮可能不可见。


我遇到了同样的问题,我使用情节提要(Xcode v9.2)解决了它

1-转到故事板,您的ViewController在哪里,选择它

Select

注意:如果它在"当前上下文"下不起作用,请选择"在当前上下文上"