我想为我的 View Controller 使用模糊背景,但一旦加载 View ,整个东西就会变成灰色。
我通过从另一个 View 执行 Segue 来呈现 View Controller ,并在 View Controller 的 viewDidLoad()
方法中,实现如下所示的模糊效果
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
let blurBgView = UIVisualEffectView(effect: UIBlurEffect(style: .extraLight))
blurBgView.translatesAutoresizingMaskIntoConstraints = false
view.insertSubview(blurBgView, at: 0)
NSLayoutConstraint.activate([
blurBgView.heightAnchor.constraint(equalTo: view.heightAnchor),
blurBgView.widthAnchor.constraint(equalTo: view.widthAnchor),
])
}
这就是它的样子
如何为我的 View Controller 实现模糊背景?
我使用 XCode 9.3、Swift 4.1
谢谢
最佳答案
展示你的 View Controller 并从你的初始VC中设置它的modalPresentationStyle
:
let newController = NewViewController()
newController.modalPresentationStyle = .overCurrentContext
present(newController, animated: true, completion: nil)
或者,如果您正在准备继续:
let newController: NewViewController = segue.destination as! NewViewController
newController.modalPresentationStyle = .overCurrentContext
并在 viewDidLoad()
的 NewViewController
类中添加您的代码。
关于ios - UIViewController模糊背景变成灰色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52495899/