简体   繁体   中英

Dismissing of UIImagePickerController dismisses presenting view controller also

I am working on a project that uses tabbar system. One of the item of tabbar is JobPostingViewController. I Embed it in UINavigationController. There is a UIButton called add new job in this view controller .I implemented pushviewcontroller to go CreateJobPostViewController. There I need to add UIImagePickerController to choose the image. When i tap done button or choose an image from library it dismisses to JobPostingViewController. But it should go to the CreateJobPostViewController. Any one please help me. Thanks in advance.

You can see the issue here:
在此处输入图片说明

Code in JobPostingViewController

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

Code in CreateJobPostViewController

   @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”来修复该问题:

picker.modalPresentationStyle = .overCurrentContext

Adding Picker as subview

try to add the imagepicker as a subview to your CreateJobPostViewController insted of presenting it and then remove it from parent in the delegtes

@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!)
}

and then

 func imagePickerControllerDidCancel(picker: UIImagePickerController) {

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

For presenting

showing picker over currentcontext with options like edit cancel choose,

use picker.modalPresentationStyle = .overCurrentContext //before presenting it

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

You could use:

picker.modalPresentationStyle = .overCurrentContext

but depending on your screen layout, it may be better to use:

picker.modalPresentationStyle = .overFullScreen

otherwise your "cancel", "retake" and "use photo" buttons may not be visible.

I had this same problem, I solved it using the storyboard (Xcode v9.2)

1 - Go to the storyboard, where is your ViewController, select it

选择视图控制器

2 - Set Presentation as: Current Context

设置当前上下文

Note: If it does not work Current Context, select Over Current Context

I was trying to present the picker over an iOS 13 popover view controller. To stop the camera from dismissing my popover view controller I had to change the picker's modalPresentationStyle to popover. See below:

picker.modalPresentationStyle = .popover

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM