Swift模态视图控制器具有透明背景 [英] Swift Modal View Controller with transparent background

查看:1000
本文介绍了Swift模态视图控制器具有透明背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个主题很受欢迎,但我在编程语言中有点问题,事实是我仍然不明白我把代码放在哪里。好吧,我将告诉整个案例:

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:

我正在尝试使模态Swift与正常情况略有不同:通过单击按钮,ViewController将在屏幕上显示(以下模式类型),但背景为透明。仅显示带标签的蓝色视图。当呈现此ViewController时,它具有透明背景,但一旦完成转换,它将保持黑色背景。已经停用了不透明选项,并测试了一些选项,但没有进行任何此类故障排除。

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.

有些人可以帮助我吗?

该视频是对案例模拟器的测试( https://www.youtube.com/watch?v=wT8Uwmq9yqY )。

The video is a test in the simulator on the case (https://www.youtube.com/watch?v=wT8Uwmq9yqY).

我开始使用swift,我仍然对如何在Xcode中编程感到很遗憾,我读到了一个问题的答案有以下代码来解决这个问题:

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:

self.presentingViewController.providesPresentationContextTransitionStyle = YES;
self.presentingViewController.definesPresentationContext = YES;
modal.modalPresentationStyle = UIModalPresentationOverCurrentContext;

我在哪里放这个代码?

推荐答案

你可以这样做:

在你的主视图控制器中:

func showModal() {
    let modalViewController = ModalViewController()
    modalViewController.modalPresentationStyle = .overCurrentContext
    presentViewController(modalViewController, animated: true, completion: nil)
}

在你的模态视图控制器:

class ModalViewController: UIViewController {
    override func viewDidLoad() {
        view.backgroundColor = UIColor.clearColor()
        view.opaque = false
    }
}

如果您正在处理故事板:

只需添加一个故事板Segue与种类设置为向模态视图控制器提供模态 d在此视图控制器上设置以下值:

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:


  • 背景=清除颜色

  • 绘图=取消选中不透明复选框

  • Presentation = Over Current Context

as Crashalot 在他的评论中指出:确保 segue 仅使用默认对于演示文稿过渡。使用当前上下文获取演示文稿使模态变为黑色而不是保持透明。

As Crashalot pointed out in his comment: Make sure the segue only uses Default for both Presentation and Transition. Using Current Context for Presentation makes the modal turn black instead of remaining transparent.

这篇关于Swift模态视图控制器具有透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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