自定义模式对话框

来源:互联网 发布:软件项目估计方法 编辑:程序博客网 时间:2024/06/18 04:01

首先在故事板中拖入一个UIViewController,设计你的UI。我们以下面的例子为例:

在这个ViewController中,上半部分我们将设置为透明(通过代码设置),而下半部分是3个UIButton。

在Identity面板中,将Class 设置为一个新的UIViewController子类。然后创建这个UIViewController子类。

在故事板中,选中3个按钮所在的父UIView,即上图中的containerView,添加如下布局约束:

打开助手编辑器,为最下面一个约束Bottom Space to在创建IBOutlet menuBottomConstraint。
并在viewDidLoad方法中,加入代码:

self.view.backgroundColor = UIColor(white: 0.3, alpha: 0.3)
menuBottomConstraint.constant = -128

在viewDidAppear方法中:
super.viewDidAppear(animated)
self.view.layoutIfNeeded()

UIView.animateWithDuration(0.2) {
self.menuBottomConstraint.constant = 0
self.view.layoutIfNeeded()
}

然后在故事板中,在需要显示这个对话框的Scene中,创建一个segue导航到这个我们的自定义对话框。选择segue,在属性面板中进行如下设置(注意,Animates设置为false,Presentation和Transition要设置为Default,否则背景不会透明):


在需要显示对话框的ViewController的prepareForSegue方法中:
if segue.identifier == “ContactUsController” {
if let vc=segue.destinationViewController as? UIViewController{
vc.modalPresentationStyle =
UIModalPresentationStyle.OverCurrentContext;

}

}

运行程序,效果如下:

0 0
原创粉丝点击