iosswiftipaduipopoveruipresentationcontroller

UIPopoverPresentationController on iPad


I want to present a viewController in UIPopoverPresentationController like iOS Setting App do when we choose language. It presents a viewController at center of device screen! enter image description here

But, my result is not like that!

enter image description here

This is my code:

func rightButtonPressed(rightBarButton: UIBarButtonItem) {
    let detailVC = self.storyboard?.instantiateViewControllerWithIdentifier("ExampleViewController") as! ExampleViewController
    detailVC.modalPresentationStyle = .Popover
    let popOverVC = detailVC.popoverPresentationController
    popOverVC?.permittedArrowDirections = .Any
    popOverVC?.barButtonItem = rightBarButton
    popOverVC?.sourceView = self.view
    popOverVC?.delegate = self

    self.presentViewController(detailVC, animated: true, completion: nil)
}

And the delegate:

extension DetailViewController: UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        debugPrint(controller.description)
        return .Popover
    }

    func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
        let navController = UINavigationController.init(rootViewController: controller.presentedViewController)
        return navController 
    }
}

Solution

  • Try setting the presentation style to sheet:

    detailVC.modalPresentationStyle = UIModalPresentation.FormSheet