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