简体   繁体   English

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

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

I want to present ViewController as a popup ViewController, it's working good but the problem is background color getting black, but I want a transparent background color.我想将 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)

With this code, I can present a transparent popup ViewCcontroller but when click on the close button in popup ViewController viewWillAppear() not calling.使用此代码,我可以呈现一个透明的弹出式 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 ,希望它起作用!

It is not possible to make UIView transparent, until it is subview of some other UIVIew.不可能使 UIView 透明,除非它是其他 UIVIew 的子视图。

To achieve what you are trying, you can add that view controller as child view controller of presenter.为了实现您的尝试,您可以将该视图控制器添加为演示者的子视图控制器。 and add child view controller's view as subview to parent view controller's view.并将子视图控制器的视图作为子视图添加到父视图控制器的视图中。

Have a look: let childViewController be the one you want to present.看一看:让 childViewController 成为您想要展示的那个。 then do this in presenter(parent) view controller.然后在演示者(父)视图控制器中执行此操作。

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

You can add animations as well.您也可以添加动画。 Hope this will help.希望这会有所帮助。

If you would like to present your ViewController as popup.如果您想将 ViewController 显示为弹出窗口。 you can use the UIWindow + modalPresentationStyle + modalTransitionStyle.您可以使用 UIWindow + modalPresentationStyle + modalTransitionStyle。 like that:像那样:

 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.

相关问题 如何使Popup ViewController的背景透明? - How to make a transparent background of Popup ViewController? 如何在 Swift 中呈现 VC 并设置为新的 Root viewController? - How can I present VC and set as new Root viewController in Swift? 如何推送具有透明背景的新viewController? - How to push a new viewController with Transparent Background? 如何将一个ViewController或View加载到具有透明背景的另一个ViewController中? - How can I load a ViewController or View into another ViewController with transparent background? 推ViewController与透明背景 - Push ViewController with transparent background 情节提要ViewController中的透明背景 - transparent background in storyboard viewcontroller 当我仍然可以看到背景viewController时,如何呈现viewController - How to present viewController while I can still see the background viewController 如何在仍然看到背景viewController的同时显示viewController - How could I present a viewController while still see the background viewController 如何在iOS中呈现半透明(半切)视图控制器? - How to present a semi-transparent (half-cut) viewcontroller in iOS? 如何使用离子(v1)框架呈现弹出viewController? - How to present a popup viewController using ionic (v1) framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM