xcodeswiftios8uipresentationcontroller

UIPopoverPresentationController


I'm new to swift and trying to understand how to use UIPopoverPresentationController.

What I need in my app is that when a button is pressed a popover from the button of the screen will be shown on with an xib file on half of the screen . What happens now is that I manage to segue to the second view controller but don't know how to load the xib file and how to make the popover to be an half of the screen from the button. this is my code

import UIKit

class BaseViewController: UIViewController , UIPopoverPresentationControllerDelegate {

    @IBAction func moveToPopoverView(sender: UIButton) {
      var popoverViewController = self.storyboard?.instantiateViewControllerWithIdentifier("Popover") as! UIViewController
      popoverViewController.modalPresentationStyle = .Popover
      popoverViewController.preferredContentSize   = CGSizeMake(200, 200)

      let popoverPresentationViewController = popoverViewController.popoverPresentationController

      popoverPresentationViewController?.permittedArrowDirections = .Any
      popoverPresentationViewController?.delegate = self

      presentViewController(popoverViewController, animated: true, completion: nil)
    }

   override func viewDidLoad() {
      super.viewDidLoad()       
   }    
}

Solution

  • The easiest way to accomplish this is via a storyboard popover presentation segue. It will handle the presented view controller instantiation for you.

    1. Add a popover presentation segue to the button that will present the popover. Give the segue an identifier. This will handle anchoring the popover to the button:

    Popover presentation segue from (titleView) button

    1. Set the storyboard presentation details for the presented view controller. I've selected Page Sheet presentation for a tableView controller (that's embedded in a navigation controller).

    (Page sheet) presented view controller

    1. Add the prepareForSegue:sender: code to pass any parameters to your presented view controller. (I haven't learned Swift... sorry for the Objective C.)
        - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)__unused sender {
            if ([[segue identifier] isEqualToString:@"showBIBLESelectBookChapter"]) {
                UINavigationController *navigationController = segue.destinationViewController;
                if ([navigationController.topViewController isKindOfClass:[BIBLESelectViewController class]]) {
                    BIBLESelectViewController *selectViewController = (BIBLESelectViewController *)navigationController.topViewController;
                    selectViewController.initialBookChapterVerse = self.bookChapterVerse;
                }
                UIPopoverPresentationController *popoverPresentationController = navigationController.popoverPresentationController;
                popoverPresentationController.delegate = self;
            }
        }

    The segue will do all the actual work for you, instantiating, presenting, and anchoring the popover view controller.

    Presented popover view controller

    You should be able to set your content size so your popover is half-screen.