Popup UIViewController

I am trying to make a popup that will be presented with a click of a button. I tried to follow the instructions that I found on Google, but my pop view, presented in full screen, and its background is black. Here is my code:

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate { @IBAction func someButtonPressed(sender: UIButton) { let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let popupVC = storyboard.instantiateViewControllerWithIdentifier("hello") as! popupViewController popupVC.modalPresentationStyle = .Popover popupVC.preferredContentSize = CGSizeMake(300, 300) let pVC = popupVC.popoverPresentationController pVC?.permittedArrowDirections = .Any pVC?.delegate = self pVC?.sourceView = sender pVC?.sourceRect = CGRect(x: 100, y: 100, width: 1, height: 1) presentViewController(popupVC, animated: true, completion: nil) } } 

What am I doing wrong?

+9
source share
3 answers

In order for your view controller to display as a popup, you must set the following:

 popupVC.modalPresentationStyle = .OverCurrentContext popupVC.modalTransitionStyle = .CrossDissolve 

You must also create your controller position, size, so that it looks like a popup.

Here is my popup that I did before.

enter image description here

+24
source

On the parent controller:

 let vc = ViewController() vc.modalPresentationStyle = .overCurrentContext vc.modalTransitionStyle = .crossDissolve present(vc, animated: true, completion: nil) 

On a popup controller, use this to enable the parent controller to be shown in the background:

 self.definesPresentationContext = true 

Remember to set a translucent background for your popup controller.

- Link from: Popup controller view programmatically - Reddit -

0
source

Another simple solution using EzPopup ( https://github.com/huynguyencong/EzPopup ). It is very simple, just a few lines of code:

 // init YourViewController let contentVC = ... // Init popup view controller with content is your content view controller let popupVC = PopupViewController(contentController: contentVC, popupWidth: 100, popupHeight: 200) // show it by call present(_ , animated:) method from a current UIViewController present(popupVC, animated: true) 
-1
source

All Articles