UIPopoverPresentationController

I'm new to quick and trying to figure out how to use UIPopoverPresentationController

.

What I need in my application is that when the button is pressed, the pop bar from the button on the screen will be displayed with an xib file on half of the screen. What's happening now is that I manage to navigate to the second view controller but don't know how to load the xib file and how to make the popover half 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()       
   }    
}

      

+3


source to share


1 answer


The easiest way to achieve this is through a segue presentation. It will handle the view controller instance you created.

  • Add a demo presentation to the button that will present the popover. Give the segue an id. This will bind the popover to the button:

Popover presentation segue from (titleView) button

  1. Set the presentation details of the storyboard for the presented view controller. I selected the Page Sheet presentation for the tableView controller (built into the navigation controller).

(Page sheet) presented view controller



  1. Add code prepareForSegue:sender:

    to pass any parameters to your presented view controller. (I didn't recognize Swift ... sorry for 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;
        }
    }

Seg will do all of your work, instantiating, presenting and committing the popover view controller.

Presented popover view controller

You must set the size of your content so your popover is half screened.

+5


source







All Articles