首页 > 解决方案 > 如何将 ViewController 呈现为具有透明背景的弹出式 VC?

问题描述

我想将 ViewController 呈现为弹出式 ViewController,它工作得很好,但问题是背景颜色变黑,但我想要一个透明的背景颜色。

let vc = self.storyboard?.instantiateViewController(withIdentifier: "ADLVC") as! ListViewController
//remove black screen in background
vc.modalPresentationStyle = .overCurrentContext
//add clear color background
vc.view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
//present modal
self.navigationController?.pushViewController(vc, animated: false)

使用此代码,我可以呈现一个透明的弹出 ViewCcontroller,但是当单击弹出 ViewController viewWillAppear()中的关闭按钮时不会调用。

//create view controller
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SVC") as! SViewController

    //remove black screen in background
    vc.modalPresentationStyle = .overCurrentContext
    //add clear color background
    vc.view.backgroundColor = UIColor.black.withAlphaComponent(0.4)

    //present modal
    self.present(vc, animated: false, completion: nil)

标签: iosswiftxcodecocoa-touchuiviewcontroller

解决方案


在vc中,用于[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]设置背景颜色和删除vc.modalPresentationStyle = .overCurrentContext,希望它有效!


推荐阅读