how to center a popoverview in swift
You need to provide the source rect for the popover.
From the apple documentation: the source rect is the rectangle in the specified view in which to anchor the popover. Use this property in conjunction with the sourceView property to specify the anchor location for the popover.
In your case, under
_popoverPresentationController.sourceView = self.view;
add:
_popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds),0,0)
It will do the trick!
Here's an implementation using Swift 3
let popover = storyboard?.instantiateViewController(withIdentifier: "popover") as! PopoverVC popover.modalPresentationStyle = UIModalPresentationStyle.popover popover.popoverPresentationController?.backgroundColor = UIColor.green popover.popoverPresentationController?.delegate = self popover.popoverPresentationController?.sourceView = self.view popover.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) popover.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0) self.present(popover, animated: true)
Based on Istvan'sanswer
Swift 4 implementation :
popover.popoverPresentationController?.sourceRect = CGRect(x: view.center.x, y: view.center.y, width: 0, height: 0)popover.popoverPresentationController?.sourceView = viewpopover.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)