Zizheng Wu Zizheng Wu - 10 days ago 20
Swift Question

How to present a UIViewController as a popover in Swift programmatically on iPhone

Generally I followed the instructions here: http://stackoverflow.com/a/24687152/3741933.

However, as discussed in its comments, the popover is always fullscreen regardless of


The button to present the popover:

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)

The UIViewController:

import UIKit

class EmptyViewController : UIViewController {
override func viewDidLoad() {
view.backgroundColor = UIColor.redColor()

enter image description here

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.


You can't do this in iPhone with portrait mode with this code ... you can check popover section in apple doc here..

It suggests that:

In iOS 8 and later, you use a UIPopoverPresentationController to present a popover. UIPopoverPresentationController defines a delegate that lets you adjust the display style of your popover content to suit the current display environment. For example, in a horizontally regular environment, your content can display inside a popover; in a horizontally compact environment, your content can display in a full-screen modal view.

And as I said, if you can check in iPad, your content can display inside a popover.