如何在swift中使用模态视图? [英] How to use modal views in swift?
问题描述
向Mail添加帐户(在首选项中)时,您会看到一个模态视图,如下所示:
When adding an account to Mail (in preferences), you get a modal view, like this:
我的问题是,如何以编程方式复制此代码?换句话说,如何在呈现视图上显示模态UIView?
My question is, how do I replicate this programatically? In other words, how do I display a modal UIView over a presenting view?
这是我所拥有的:
import UIKit
class ViewController: UIViewController {
@IBAction func addCard(sender: AnyObject) {
var addContact : secondViewController = secondViewController()
self.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
self.modalPresentationStyle = .CurrentContext // Display on top of current UIView
self.presentViewController(addContact, animated: true, completion: nil)
}
//Code goes on to do other unrelated things
此外,我已完成以下操作:
Also, I've done the following:
- 在界面构建器中创建视图控制器。
- 将BarButtonItem添加联系人连接到viewcontroller,通过按住Control键并单击,拖动到我想要显示的视图的viewcontroller,然后在下拉列表中选择modal 。
- 将呈现的Viewcontroller的Class和StoryBoard UI设置为secondViewController。
预期行为是,当按下UIBarButtonAdd Contact(在上面的代码中成功触发 @IBAction func addCard(sender:AnyObject)
)时,secondViewController呈现为主视图上方的模态视图。
The expected behavior is that, when the UIBarButton "Add Contact" (which successfully triggers @IBAction func addCard(sender: AnyObject)
in the code above) is pressed, secondViewController is presented as a modal view above the main view.
当我运行上面的代码时,我收到错误使用未声明的类型secondViewController
When I run the code above, I get the error "Use of undeclared type secondViewController"
我做错了什么?
注意:这是对一个早期的问题,其中我问了一个类似但略有不同的问题。我查看了 meta ,我认为是好的 - 我不想让原始问题的答案无效,因为这是在问一些稍微不同的东西。另外,如果它有帮助,我发现了一些类似的问题 - 在obj C中我如何在swift中做到这一点?
NOTE: This is a re-ask of an earlier question in which I asked a similar, but slightly different question. I checked on meta, and I think it is OK - I don't want to invalidate the answers on the original question, as this is asking something slightly different. Also, in case it helps, I found some similar questions - in obj C. How do I do that in swift?
推荐答案
试试这个:)
@IBAction func addCard(sender: AnyObject) {
self.modalTransitionStyle = UIModalTransitionStyle.coverVertical
// Cover Vertical is necessary for CurrentContext
self.modalPresentationStyle = .currentContext
// Display on top of current UIView
self.present(secondViewController(), animated: true, completion: nil)
}
这篇关于如何在swift中使用模态视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!