The solution I implemented for this is based on the example presented at the 2014 WWDC session Viewing controller enhancements in iOS 8 "(see slide notes ). Note that you need to implement the adaptivePresentationStyleForPresentationController function as part of the UIPopoverPresentationControllerDelegate , but this function should located outside your function sendTapped in your main view controller, and you must specify UIPopoverPresentationControllerDelegate in its class declaration in the file to make sure that your code changes this behavior. I also took the liberty of elit logic to present the view controller in a popover in its own function and added checks to make sure that the function is not the controller of the request, if it is already present in the current context.
So your solution might look something like this:
// ViewController must implement UIPopoverPresentationControllerDelegate class TheViewController: UIViewController, UIPopoverPresentationControllerDelegate { // ... // The contents of TheViewController class // ... @IBAction func sendTapped(sender: UIBarButtonItem) { let popView = PopViewController(nibName: "PopView", bundle: nil) self.presentViewControllerAsPopover(popView, barButtonItem: sender) } func presentViewControllerAsPopover(viewController: UIViewController, barButtonItem: UIBarButtonItem) { if let presentedVC = self.presentedViewController { if presentedVC.nibName == viewController.nibName { // The view is already being presented return } } // Specify presentation style first (makes the popoverPresentationController property available) viewController.modalPresentationStyle = .Popover let viewPresentationController = viewController.popoverPresentationController? if let presentationController = viewPresentationController { presentationController.delegate = self presentationController.barButtonItem = barButtonItem presentationController.permittedArrowDirections = .Up } viewController.preferredContentSize = CGSize(width: 30, height: 30) self.presentViewController(viewController, animated: true, completion: nil) } func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle { return .None } }
Real world implementation
I took this approach to validate the input on the registration form in an unfinished application that I host on Github. I implemented it as extensions to UIVIewController in UIViewController+Extensions.swift . You can see it in use in the validation functions in AuthViewController.swift . The presentAlertPopover method takes a string and uses it to set the UILabel value to the GenericAlertViewController that I set (it makes it easy to have dynamic tooltips). But the actual magic of popover all happens in the presentViewControllerAsPopover method , which takes two parameters: an instance of the UIVIewController to be presented, and a UIView to use as an anchor from which the popover can be represented. The direction of the arrow is hardcoded as UIPopoverArrowDirection.Up , but that would be hard to change.
source share