ios 弹出 UIViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37205789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Popup UIViewController
提问by Ookey
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?
我做错了什么?
回答by Tien
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.
这是我之前做的弹出窗口。
回答by dianakarenms
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 backgroundto your pop up controller.
不要忘记为弹出控制器设置半透明背景。
-- Reference from: Presenting a popup view controller programmatically - Reddit--
-- 参考:以编程方式呈现弹出视图控制器-Reddit--
回答by huync
Another simple solution, using EzPopup (https://github.com/huynguyencong/EzPopup). It's very simple with just a few lines of code:
另一个简单的解决方案,使用 EzPopup ( https://github.com/huynguyencong/EzPopup)。非常简单,只需几行代码:
// 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)