具有透明背景的快速模态视图控制器

我知道这个主题相当流行,但是我在编程语言中是一个小问题,事实是我仍然不明白我把代码放在哪里。好吧,我会告诉整个案子:

enter image description here

我正在尝试做一个有点不同于普通模式的 Swift: 通过点击一个按钮,ViewController 会显示在屏幕上(模式类型) ,但是背景是透明的。只显示带标签的蓝色视图。当这个 ViewController 出现时,它的背景是透明的,但是一旦它完成转换,它将保持黑色背景。已经停用了不透明选项,并测试了一些选项,但没有这个故障排除。

有人能帮我吗?

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

我从“迅速”开始,但是我仍然对如何用 Xcode 编程感到迷惑,我读到了一个问题的答案,这个问题有以下代码可以解决:

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

我该把密码放在哪里?

139280 次浏览

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:

  • Background = Clear Color
  • Drawing = Uncheck the Opaque checkbox
  • Presentation = Over Current Context

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.