Search code examples
iosswiftpopoverios9

Present UISearchBar results as UIPopoverPresentationController iOS 9


I have a ParentViewController with a UISearchBar, and a ChildTableViewController with a tableView where the results from the search will be displayed. When I tap on the searchBar, a popover should present with all the results that conform to the filter written on the searchBar. This means, at the beginning, all results should be displayed in a popoverController.

The problem is that the results are shown occupying the whole screen, instead of being presented in a popover. Below is the code corresponding to ParentViewController where the popover should be presented.

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    let childController = ChildTableViewController()
    childController.modalPresentationStyle = .Popover
    childController.preferredContentSize = CGSize(width: 50, height: 100)
    let popover = childController.popoverPresentationController
    popover?.permittedArrowDirections = .Any
    popover?.delegate = self
    popover?.sourceView = self.view
    popover?.sourceRect = CGRect(x: 200, y: 200, width: 1, height: 1)
    presentViewController(childController, animated: true,completion: nil)

}

enter image description here

I am using Xcode 7 and iOS 9.


Solution

  • What you need to do is to implement the following method which is part of the UIPopoverPresentationControllerDelegate protocol:

    - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
        return UIModalPresentationNone; // This will force a popover display
    }