Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPopoverPresentationController displaying popover as full screen

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
}
like image 240
The Nomad Avatar asked Dec 08 '14 07:12

The Nomad


Video Answer


3 Answers

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
}
like image 137
gabbler Avatar answered Oct 08 '22 13:10

gabbler


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

like image 34
karthik sarpatwari Avatar answered Oct 08 '22 15:10

karthik sarpatwari


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
   }

 }
like image 38
mourodrigo Avatar answered Oct 08 '22 13:10

mourodrigo