Swift - UIPopoverController in iOS 8

I am trying to add a simple popoverController to my iphone application, and now I am struggling with the classic "blank screen" that covers everything when I click the button.

My code is as follows:

@IBAction func sendTapped(sender: UIBarButtonItem) { var popView = PopViewController(nibName: "PopView", bundle: nil) var popController = UIPopoverController(contentViewController: popView) popController.popoverContentSize = CGSize(width: 3, height: 3) popController.presentPopoverFromBarButtonItem(sendTappedOutl, permittedArrowDirections: UIPopoverArrowDirection.Up, animated: true) func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle { // Return no adaptive presentation style, use default presentation behaviour return .None } } 

The adaptivePresentationStyleForPresentationController function was just added by me because I read somewhere that this is what you need to implement in order to get this function on iphone. But still: the image is still blank on the screen, and I don’t know how to fix it.

Any suggestions would be appreciated.

+5
source share
1 answer

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.

+6
source

Source: https://habr.com/ru/post/1209963/


All Articles