UIPopoverPresentationController displaying popover as full screen

21,039

Solution 1

In iPhone, you should add the following in order to present a popover.

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
    // Return no adaptive presentation style, use default presentation behaviour
    return .None
}

Solution 2

For Swift3/IOS10, looks like we need to do some thing like

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle
{
    return .none
}

Adding this answer, in case, someone runs into this problem as i did when migrating to swift3/IOS10

Solution 3

For Swift3+/IOS10+, when dealing with iPhone:

You must add UIPopoverPresentationControllerDelegate the delegate at:

class YourClass:  UIViewController, UIPopoverPresentationControllerDelegate { ...

Then implement in this same parent class (which will show the popover) the method below.

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle
{
    return .none
}

And then set the popover configuration below:

myPopover.modalPresentationStyle = .popover
myPopover.popoverPresentationController?.sourceRect = VIEWTOPOINTTHEARROW.frame
myPopover.popoverPresentationController?.sourceView = self.view
myPopover.popoverPresentationController?.delegate = self

Also you may set some configuration for the popover class

 class MyPopover: UIViewController {

 override func viewDidLoad() {
    super.viewDidLoad()
    //popover size
    self.preferredContentSize = CGSize(width: 320, height: 200) 
    //sets the arrow of the popover to same color of background
    self.popoverPresentationController?.backgroundColor = self.view.backgroundColor
   }

 }

Solution 4

The accepted answer is correct. For completeness, see Adapting Presented View Controllers to a New Style in the Apple docs:

Use the delegate’s adaptivePresentationStyleForPresentationController: method to specify a different presentation style than the default. When transitioning to a compact environment, the only supported styles are the two full-screen styles or UIModalPresentationNone. Returning UIModalPresentationNone tells the presentation controller to ignore the compact environment and continue using the previous presentation style. In the case of a popover, ignoring the change gives you the same iPad-like popover behavior on all devices.

Make sure that the required configurations from Presenting a View Controller in a Popover are met:

After setting the modal presentation style [of the presented view controller] to UIModalPresentationPopover, configure the following popover-related attributes:

  • Set the preferredContentSize property of your view controller to the desired size.
  • Set the popover anchor point using the associated UIPopoverPresentationController object, which is accessible from the view controller’s popoverPresentationController property.
  • Set only one of the following:
    • Set the barButtonItem property to a bar button item.
    • Set the sourceView and sourceRect properties to a specific region in one of your views.
Share:
21,039

Related videos on Youtube

The Nomad
Author by

The Nomad

Android & iOS Code Ninja

Updated on July 09, 2022

Comments

  • The Nomad
    The Nomad almost 2 years

    I am trying to use UIPopoverPresentationController to display a popover that doesn't take up the whole screen. I've followed many different tutorials with no luck.

    Here is my code. It correctly instantiates the ViewController, but it takes up the entire screen instead of just a smaller screen as I defined in preferredContentSize.

    func showPopover() {
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("PopupTimePickerViewController") as PopupTimePickerViewController
        vc.modalPresentationStyle = .Popover
        vc.preferredContentSize = CGSizeMake(200, 100)
    
        if let presentationController = vc.popoverPresentationController {
            presentationController.delegate = self
            presentationController.permittedArrowDirections = .Up
            presentationController.sourceView = self.view
            presentationController.sourceRect = CGRectMake(0, 0, 50, 50)
    
            self.presentViewController(vc, animated: true, completion: nil)
        }
    }
    

    Update 9/27/16 with correct answer

    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return .none
    }
    
    • The Nomad
      The Nomad over 9 years
      @gabbler Nope. Only on the simulator. Don't have an iPhone to test it on.
    • gabbler
      gabbler over 9 years
      Please try the answer. Don't forget to implement adaptivePresentationStyleForPresentationController method.
    • Alexandre
      Alexandre over 6 years
      For me the problem was that, calling the PopoverViewController through a "PerformSegue" was always leaving it Full Screen. Deleting the storyboard segue and manually assembling it and showing through presentViewController solved it.
    • MingMan
      MingMan over 5 years
      Update saved me. Was stuck for +6 hours
  • Noah
    Noah about 8 years
    Where do you add this? To the view controller of the Popover?
  • gabbler
    gabbler about 8 years
    @Noah, to the view controller that presents the Popover, self.presentViewController(vc, animated: true, completion: nil), the controller self represents.
  • Cannoliopsida
    Cannoliopsida over 6 years
    On Swift 4, this won't give an error, but just won't fix the issue. Slight signature tweak: func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle { return UIModalPresentationStyle.none }
  • Victor Engel
    Victor Engel about 5 years
    Yes, and in order for it to be called, you need to set the calling view controller as the delegate to the popover's presentationController, as explained in other answers. Then you can look at the protocol definition for the appropriate signature. Xcode should autocomplete with the right one if you've set up the delegate properly.