简体   繁体   中英

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

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!

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)

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

只需在调用imagepicker的函数中添加imagePicker.delegate = self

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