Search code examples
iosswiftuipopovercontroller

Swift Present Popover View from a UIButton


Question

Hello. How, in Swift, do I present a popover from a Storyboard-created UIButton, in order to get photos from the photo library? I have figured out the photos library stuff, just not the presenting of the view controller.

Research

I found instructions on how to do a similar thing from a BarButtonItem about half way down here.

Current Attempts

I am trying the following code.

self.imagePicker.delegate = self
self.imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
self.imagePicker.allowsEditing = false
            
self.imagePicker.modalPresentationStyle = .Popover
presentViewController(self.imagePicker, animated: true, completion: nil)//4
self.imagePicker.popoverPresentationController?.photo = sender

with photo being the UIButton that I want to present this modal view from. For simplicity's sake, only the parts of the code related to presenting the view are given.

I have also tried

self.imagePicker.delegate = self
            self.imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
            self.imagePicker.allowsEditing = false
            
            self.imagePicker.modalPresentationStyle = .Popover
            self.imagePicker.popoverPresentationController!.sourceRect = self.photo.bounds
            self.presentViewController(self.imagePicker, animated: true, completion: nil)

which gives a sigabrt,

Action

When the above code is typed, Xcode gives the error

"use of unresolved identifier 'sender'"

Expected Action

I want a popover containing the photo library to be presented, as shown in this image:

Expected Behavior

What am I doing wrong? Thanks!


Solution

  • try setting the source view of popover like this

    actionSheet.popoverPresentationController?.sourceView = photo
    

    Hope it helps.