sant05
sant05

Reputation: 428

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:
enter image description 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)
}

Upvotes: 10

Views: 9437

Answers (5)

JTODR
JTODR

Reputation: 338

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

Upvotes: 0

Ever CR
Ever CR

Reputation: 351

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

Select ViewController

2 - Set Presentation as: Current Context

Set current context

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

Upvotes: 0

Anshad Rasheed
Anshad Rasheed

Reputation: 2566

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)

Upvotes: 9

Chris
Chris

Reputation: 179

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.

Upvotes: 6

Sourabh Sharma
Sourabh Sharma

Reputation: 8322

Fixed the issue by setting the image picker's modalPresentationStyle to "OverCurrentContext":

picker.modalPresentationStyle = .overCurrentContext

Upvotes: 26

Related Questions