How to present an UIImagePickerController in a Popover with iOS 9 and Swift

i0S Swift Issue

Question or problem in the Swift programming language:

I’m trying to present the photo library, in a popover, on an iPad with iOS 9 beta 4 and Swift. The preferred way is through a popover, but UIPopoverController is now deprecated. Apparently it’s now done through UIViewController, but there is no documentation, or sample code out there that I could find. Any help would be greatly appreciated!

Thank you!

How to solve the problem:

Solution 1:

The above answer is almost correct except that the anchor in the popoverPresentationController must be set prior to calling presentViewController():

let myPicker = UIImagePickerController()
myPicker.delegate = self
myPicker.sourceType = .PhotoLibrary
myPicker.modalPresentationStyle = .Popover

let ppc = myPicker.popoverPresentationController
ppc?.barButtonItem = sender as? UIBarButtonItem
ppc?.permittedArrowDirection = .Any

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

Solution 2:

I haven’t entered into delegation and handling of the image picker responses here, the purpose of this post has simply been to consider the use of UIImagePickerController without resorting to deprecated classes and methods.

let myPicker = UIImagePickerController()
myPicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

myPicker.modalPresentationStyle = UIModalPresentationStyle.Popover

self.presentViewController(myPicker, animated: true, completion: nil)

let popper = myPicker.popoverPresentationController
// returns a UIPopoverPresentationController
popper?.barButtonItem = sender as? UIBarButtonItem

please Correct if needed

Solution 3:

simply added imagePicker.delegate = self in the function that calls up the imagepicker

Hope this helps!