2

So I am inside a navigation controller and would like to present an image picker upon a button press. That works fine, but when I dismiss the picker it throws me back to the root view controller, and not where I want to process the image.

This is my code:

@IBAction func attachPhotoButtonPressed(sender: UIButton) {
        imagePicker.sourceType = .SavedPhotosAlbum
        presentViewController(imagePicker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        print("Success")
    } else{
        print("Something went wrong")
    }

    self.dismissViewControllerAnimated(true, completion: nil)
}

I already looked at these questions, but didn't find a fix. (Not an objective-C guy)

Pushing a navigation controller is not supported- performing segues

presenting ViewController with NavigationViewController swift

Pushing a navigation controller is not supported

TimLR
  • 267
  • 2
  • 10

2 Answers2

0

The reason that you're bouncing to the root view controller is because this line

self.dismissViewControllerAnimated(true, completion: nil)

is dismissing the parent view (self), and not the picker view. So assuming that you have a reference to your picker, then you should be calling

picker.dismissViewControllerAnimated(true, completion: nil)

Chris Allwein
  • 2,498
  • 1
  • 20
  • 24
  • 1
    If it was this easy I had it figured out by now, thank you for your help, I believe it has something to do with how the navigation is structured in general (took over from somebody else) I've solved it with writing my own picker on top of the Photos Framework which works quite good. – TimLR Jul 24 '17 at 23:28
0

I just had the same problem as you and I'm doing this as an alternative :

// Custom class : see below.
private let imagePicker = ImagePickerViewCustomController() 

@IBAction func openPicker(_ sender: Any) {
    // Hide the navigation bar when the picker is opened
    navigationController!.setNavigationBarHidden(true, animated: false)
    // Add it as a subview
    addChild(imagePicker)
    view.addSubview(imagePicker.view)
}

I have a subclass of UIImagePickerController that hides the status bar. The content of the scrollView in the picker scroll over the status bar otherwise.

class ImagePickerViewCustomController: UIImagePickerController {
    override func viewDidLoad() {
        super.viewDidLoad()
        UIApplication.shared.isStatusBarHidden = true
    }
}

And finally handle delegate methods by dismissing the picker by this way :

extension UIViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
        dismissPicker(picker: picker)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismissPicker(picker: picker)
    }

    private func dismissPicker(picker : UIImagePickerController){
        picker.view!.removeFromSuperview()
        picker.removeFromParent()
        navigationController?.setNavigationBarHidden(false, animated: false)
        UIApplication.shared.isStatusBarHidden = false
    }
}
AnthonyR
  • 3,485
  • 1
  • 18
  • 41