2016-05-13 41 views
5

我试图做一个弹出窗口,将通过按下按钮呈现。试图按照我在谷歌找到的说明,但我的流行视图呈现在全屏幕和背景是黑色的。 这里是我的代码:Popup UIViewController

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) 
    } 
} 

我做错了什么?

回答

16

为了显示为一个弹出视图控制器,你应该设置如下:

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

你也应该设计自己的视图控制器的位置,大小,使它看起来像一个弹出窗口。

这是我之前做的弹出窗口。

enter image description here