且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swift模态视图控制器具有透明背景

更新时间:2023-10-24 20:15:52

你可以这样做:

在你的主视图控制器中:

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.