Popup UIViewController

自作多情 提交于 2019-12-03 12:19:47

问题


I'm trying to make a popup which will be presented by pressing a button. Tried to follow instructions which i found in google but my pop view presenting in a 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 I'am doing wrong?


回答1:


To make your view controller shown as a popup, you should set the following:

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

You should also design your view controller's position, size to make it look like a popup.

Here is my popup that i did before.




回答2:


At the parent controller:

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

At the pop up controller, use this to enable showing the parent controller in the background:

self.definesPresentationContext = true

Don't forget to set a translucent background to your pop up controller.

-- Reference from: Presenting a popup view controller programmatically - Reddit --




回答3:


Another simple solution, using EzPopup (https://github.com/huynguyencong/EzPopup). It's very simple with 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)


来源:https://stackoverflow.com/questions/37205789/popup-uiviewcontroller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!