How to present a UIViewController as a popover in Swift programmatically on iPhone
<p>Generally I followed the instructions here: <a href="https://stackoverflow.com/a/24687152/3741933">https://stackoverflow.com/a/24687152/3741933</a>.  </p>

<p>However, as discussed in its comments, the popover is always fullscreen regardless of <code>preferredContentSize</code> or <code>sourceRect</code>.</p>

<p>The button to present the popover:</p>

<pre><code>func buttonClicked(sender: UIButton!) {
    let ac = EmptyViewController() as UIViewController
    ac.modalPresentationStyle = .Popover
    ac.preferredContentSize = CGSizeMake(200, 200)
    let popover = ac.popoverPresentationController
    popover?.delegate = self
    popover?.permittedArrowDirections = .Any
    popover?.sourceView = self.view
    popover?.sourceRect = CGRect(x: 100, y: 100, width: 100, height: 100)

    presentViewController(ac, animated: true, completion: nil)
}
</code></pre>

<p>The UIViewController:</p>

<pre><code>import UIKit

class EmptyViewController : UIViewController {
    override func viewDidLoad() {
        view.backgroundColor = UIColor.redColor()
    }
}
</code></pre>

<p><a href="https://i.stack.imgur.com/flRZk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/flRZk.png" alt="enter image description here"></a></p>

<blockquote>
  <p>I am wondering how to make it a real popover (not full screen size). By the way, as @EI Captain indicated, it works perfectly on iPad but always fullscreen on iPhone.</p>
</blockquote>