将模态视图放在视图上并使背景变为灰色 [英] Putting a modal view over a view and making the background grey

查看:108
本文介绍了将模态视图放在视图上并使背景变为灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近3个小时我一直在尝试完成此操作,但我不知道如何执行此操作.谁能帮忙吗?

I have been trying to accomplish this for the last 3 hours and I could not figure out how to do this. Could anyone PLEASE help?

这就是我想要做的.当我按下一个按钮(例如Sign In按钮)时,我希望弹出一个模式视图,使其后面的视图变为灰色且无法应用.在此模式视图中,我需要几个按钮和静态标签.

So this is what I am trying to do. When I press a button, say, a Sign In button, I want a modal view to pop up that make the view behind it grey and untappable. And on this modal view, I want several button and static labels.

我已经阅读并试图理解一些资源,例如: http://makeapppie.com/2014/08/30/the-swift-swift-tutorials-adding-modal-views-and-popovers/,<一个href ="https://stackoverflow.com/questions/25223607/how-to-use-modal-views-in-swift">如何快速使用模式视图?,还有其他几个.但是,我很难理解代码.

I've read and tried to understand several resources already such as: Present modal view controller in half size parent controller, http://makeapppie.com/2014/08/30/the-swift-swift-tutorials-adding-modal-views-and-popovers/, How to use modal views in swift?, and several others. However, I have such a hard time understanding the code.

到目前为止,我已经有了这段代码,该代码应该使模态视图位于其后面的视图之上:

So far I have this code which is supposed to make the modal view be on top of the view behind it:

@IBAction func signIn(sender: AnyObject) {
    self.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
    // Cover Vertical is necessary for CurrentContext
    self.modalPresentationStyle = .CurrentContext
    // Display on top of    current UIView
    self.presentViewController(SignInViewController(), animated: true, completion: nil)
}

但是,这并没有产生我想要的效果.有人帮忙吗?

But this isn't producing the effect that I want. Anybody please help?

推荐答案

首先,创建灰色的空视图

First, create your gray empty view

func makeGrayView() -> UIView {
    var view = UIView(frame:UIScreen.mainScreen().applicationFrame)
    self.view.backgroundColor = UIColor.greyColor()
    return view
}

第二,将刚刚创建的视图设置为叠加层的背景

Second, set the view you just created as a background of your overlay

var backView = self.makeGrayView()
self.view.addSubview(backView)

这篇关于将模态视图放在视图上并使背景变为灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆