簡體   English   中英

UIViewController模糊背景變成灰色

[英]UIViewController blur background turns into gray

我想為我的視圖控制器使用模糊背景,但是一旦視圖被加載,整個東西就會變成灰色。

我通過從另一個視圖執行搜索來呈現視圖控制器,並且在視圖控制器的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),
        ])
}

看起來像這樣

在此處輸入圖片說明

如何為視圖控制器實現模糊背景?

我使用XCode 9.3,Swift 4.1

謝謝

展示您的視圖控制器,並從初始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類中添加您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM