[英]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.