ios 具有透明背景的 Swift 模态视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33104743/
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
Swift Modal View Controller with transparent background
提问by joern
I know this topic is quite popular, but I'm a little iniciate problem in a programming language, the fact is that I still do not understand where I put the code. Well, I'll tell the whole case:
我知道这个话题很受欢迎,但我在编程语言方面有点小问题,事实上我仍然不明白我把代码放在哪里。好吧,我会告诉整个案例:
I'm trying to make a modal Swift in a little different from normal: By clicking on a button, the ViewController is displayed (following modal type) on the screen, but with transparent background. Only the blue View with label will be displayed. When this ViewController is presented, it is with transparent background, but as soon as it completes the transition, it will stay with the black background. Already deactivated the opaque option, and tested some options, but nothing this troubleshooting.
我正在尝试以与正常方式略有不同的方式制作模态 Swift:通过单击按钮,ViewController 将显示在屏幕上(遵循模态类型),但背景是透明的。只会显示带有标签的蓝色视图。这个ViewController在呈现的时候,背景是透明的,但是一旦完成过渡,就会保持黑色背景。已经停用了不透明选项,并测试了一些选项,但没有任何故障排除。
Some can help me?
有人可以帮我吗?
The video is a test in the simulator on the case (https://www.youtube.com/watch?v=wT8Uwmq9yqY).
该视频是在模拟器上对案例 ( https://www.youtube.com/watch?v=wT8Uwmq9yqY) 进行的测试。
I'm starting with swift, and I'm still pretty lost with how to program in Xcode, I read an answer to a question that has the following code to solve this:
我从 swift 开始,但我仍然对如何在 Xcode 中编程感到很迷茫,我阅读了一个问题的答案,其中包含以下代码来解决这个问题:
self.presentingViewController.providesPresentationContextTransitionStyle = YES;
self.presentingViewController.definesPresentationContext = YES;
modal.modalPresentationStyle = UIModalPresentationOverCurrentContext;
Where do I put this code?
我把这段代码放在哪里?
回答by joern
You can do it like this:
你可以这样做:
In your main view controller:
在您的主视图控制器中:
func showModal() {
let modalViewController = ModalViewController()
modalViewController.modalPresentationStyle = .overCurrentContext
presentViewController(modalViewController, animated: true, completion: nil)
}
In your modal view controller:
在您的模态视图控制器中:
class ModalViewController: UIViewController {
override func viewDidLoad() {
view.backgroundColor = UIColor.clearColor()
view.opaque = false
}
}
If you are working with a storyboard:
如果您正在使用故事板:
Just add a Storyboard Segue with Kind
set to Present Modally
to your modal view controller and on this view controller set the following values:
只需将Kind
设置为的 Storyboard Segue 添加Present Modally
到您的模式视图控制器,并在此视图控制器上设置以下值:
- Background = Clear Color
- Drawing = Uncheck the Opaque checkbox
- Presentation = Over Current Context
- 背景 = 清晰的颜色
- 绘图 = 取消选中不透明复选框
- 演示 = 当前上下文
As Crashalotpointed out in his comment: Make sure the segueonly uses Default
for both Presentation
and Transition
. Using Current Context
for Presentation
makes the modal turn black instead of remaining transparent.
正如Crashalot在他的评论中指出的那样:确保segue仅Default
用于Presentation
和Transition
。使用Current Context
forPresentation
使模态变黑而不是保持透明。