Search code examples
iosswiftswift2ios9

iOS 9 Presenting Popover takes full screen


I am trying to present a PopOver view controller, specifically to show a small filters screen next to a TextField. However it is showing as a full-screen view controller. filters_button is the one that should trigger the pop-over. Any ideas why this is showing full screen as if it were a normal ViewController?

func showFilters(){
    let tableViewController = UITableViewController()
    tableViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
    tableViewController.preferredContentSize = CGSizeMake(20, 20)

    presentViewController(tableViewController, animated: true, completion: nil)

    let popoverPresentationController = tableViewController.popoverPresentationController
    popoverPresentationController?.sourceView = filters_button
    popoverPresentationController?.sourceRect = CGRectMake(0, 0, filters_button.frame.size.width, filters_button.frame.size.height)
}

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return .None
}

Note: At the top of my class I declare that it conforms the "UIPopoverPresentationControllerDelegate" protocol


Solution

  • Fixed: Given that for the PopOver to work on iPhone devices, you need to set the delegate of popoverPresentationController before the viewController is presented, that way the method below gets called by the delegate. So add

        popoverPresentationController?.delegate = self
    

    below

        popoverPresentationController?.sourceRect = filters_button.frame
    

    and move

        self.presentViewController(filtersVC, animated: true, completion: nil)
    

    to the end of the function.