简体   繁体   English

UIViewController模糊背景变成灰色

[英]UIViewController blur background turns into gray

I would like to use blur background for my view controller but as soon as the view is loaded, the whole thing turns into gray. 我想为我的视图控制器使用模糊背景,但是一旦视图被加载,整个东西就会变成灰色。

I present the view controller by performing a segue from another view, and in the viewDidLoad() method of the view controller, I implement the blur effect as given below 我通过从另一个视图执行搜索来呈现视图控制器,并且在视图控制器的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),
        ])
}

This is how it looks like 看起来像这样

在此处输入图片说明

How can I implement a blur background for my view controller? 如何为视图控制器实现模糊背景?

I use XCode 9.3, Swift 4.1 我使用XCode 9.3,Swift 4.1

Thanks 谢谢

Present your view controller and set its modalPresentationStyle from your initial vc: 展示您的视图控制器,并从初始vc设置其modalPresentationStyle

let newController = NewViewController()
newController.modalPresentationStyle = .overCurrentContext
present(newController, animated: true, completion: nil)

Or, if you are preparing for the segue: 或者,如果您正在准备进行测试:

let newController: NewViewController = segue.destination as! NewViewController
newController.modalPresentationStyle = .overCurrentContext

And in the NewViewController class in viewDidLoad() add your code. 并在viewDidLoad()NewViewController类中添加您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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