繁体   English   中英

如何将 ViewController 呈现为具有透明背景的弹出式 VC?

[英]How to present ViewController as popup VC with transparent background?

我想将 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)

在 vc 中,使用[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]设置背景颜色并删除vc.modalPresentationStyle = .overCurrentContext ,希望它起作用!

不可能使 UIView 透明,除非它是其他 UIVIew 的子视图。

为了实现您的尝试,您可以将该视图控制器添加为演示者的子视图控制器。 并将子视图控制器的视图作为子视图添加到父视图控制器的视图中。

看一看:让 childViewController 成为您想要展示的那个。 然后在演示者(父)视图控制器中执行此操作。

presenter.addChild(childViewController) childViewController.view.backgroundColor = UIColor.clear presenter.view.addSubView(childViewController.view) childViewController.didMove(toParent: presenter)

您也可以添加动画。 希望这会有所帮助。

如果您想将 ViewController 显示为弹出窗口。 您可以使用 UIWindow + modalPresentationStyle + modalTransitionStyle。 像那样:

 let storyboard = UIStoryboard(name: "ADLVC", bundle: nil)
    let listViewController = storyboard.instantiateViewController(withIdentifier: "ListViewController")
    listViewController.modalPresentationStyle = .overFullScreen
    listViewController.modalTransitionStyle = .crossDissolve
    self.window?.rootViewController!.present(metarTAFVC, animated: true, completion: nil)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM