0

I am currently working on an app that requieres the user profiles to be shown as a pop-over when the user picture is pressed. I managed to get this behaviour using UIPopoverPresentationControllerDelegate but I would like your help entering this pop-over in the middle of the screen.

current and desired behaviour

My code looks like:

    func presentProfile(sender: UIButton){

            let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("profileVC") as! ProfileViewController

            vc.profilePicture =  (self.contentIdToImage[content.hostId as! String]?.image)!

            vc.modalPresentationStyle = UIModalPresentationStyle.Popover

            let height = self.view.bounds.size.height
            let width = self.view.bounds.size.width

            vc.preferredContentSize = CGSizeMake(width * 0.8, height * 0.8)

            if let presentationController = vc.popoverPresentationController {
                presentationController.delegate = self
                presentationController.permittedArrowDirections =  UIPopoverArrowDirection(rawValue: 0)
                presentationController.sourceView = sender

                self.presentViewController(vc, animated: true, completion: nil)
            }

    }

Thank you

Edit:

Based on the first answer I received I started playing around with CGRect and I think the desired behaviour can be achieved using the "sourceRect" property of the UIPopoverPresentationController but I have not figure it out.

Caponte
  • 401
  • 1
  • 11
  • 20

1 Answers1

1

set the frame after you set the width & height

  let height = self.view.bounds.size.height
  let width = self.view.bounds.size.width
  vc.preferredContentSize = CGSizeMake(width * 0.8, height * 0.8)
  vc.frame = CGRect(x: CGRectGetMidX(width - vc.bounds.width / 2), y: CGRectGetMidY(height - vc.bounds.height/2), width: width * 0.8,  height: height * 0.8)
Özgür Ersil
  • 6,909
  • 3
  • 19
  • 29
  • 1
    Sorry for the late reply, I did tried this out but it did not work. I found in this post: http://stackoverflow.com/a/31854391/1270603 – Caponte Jul 28 '16 at 13:36