之前用:
let filterVC = FilterCustomerViewController()
self.presentViewController(filterVC, animated: true, completion: nil)
self.view.backgroundColor = UIColor(hexString: "#C0C0C0", alpha: 0.5
但是沒生效:
右邊不是透明的。
swift viewcontroller half transparent
用代碼:
self.view.backgroundColor = UIColor(hexString: "#C0C0C0")
self.view.alpha = 0.5
結果:
整個頁面,全部都是蒙蒙的,且沒有透明
不是我要的效果
最後用代碼:
let filterVC = FilterCustomerViewController()
filterVC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
self.presentViewController(filterVC, animated: true, completion: nil)
class FilterCustomerViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(hexString: "#C0C0C0", alpha: 0.5)
}
達到了效果:
-》即,左邊正常顯示内容,右邊部分是透明的
-》即,在原有基礎上:
調用顯示ViewController的時候,加上:
filterVC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
然後被調用的VC中的背景色,使用帶alpha的color:
self.view.backgroundColor = UIColor(hexString: "#C0C0C0", alpha: 0.5)
即可。
[後記]
後來改為:
// filterVC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
filterVC.modalPresentationStyle = UIModalPresentationStyle.Custom
使的,前面的view沒有到頂顯示的問題,解決了: