简体   繁体   中英

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.

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.

//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.

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. 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. you can use the 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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