视图上的模糊背景无法始终如一地工作

Blurred Background on View not Working Consistently

使用以下代码使视图的背景变得模糊无法始终如一地工作。

func makeBackgroundBlurry () {
    var blurEffect = UIBlurEffect()
    blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = view.bounds //view is self.view in a UIViewController
    view.insertSubview(blurEffectView, atIndex: 0)
    view.backgroundColor = UIColor.clearColor()


    //add auto layout constraints so that the blur fills the screen upon rotating device
    blurEffectView.setTranslatesAutoresizingMaskIntoConstraints(false)
    view.addConstraint(NSLayoutConstraint(item: blurEffectView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: blurEffectView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: blurEffectView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: blurEffectView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
}

正在将背景模糊应用于以模态方式呈现的视图控制器,如下所示:

@IBAction func editTitleButtonAction(sender: UIButton) {
        let vc = FieldEditor(nibName: "FieldEditor", bundle: nil)
        vc.labelOne = "Make / Manufacturer"
        vc.valueOne = item.manufacturer
        vc.labelTwo = "Model / Item Name"
        vc.valueTwo = item.name
        vc.delegate = self
        self.presentViewController(vc, animated: true, completion:nil)

    }

偶尔,它会按预期工作,在背景上产生光模糊效果;然而,大多数时候背景是模糊的,但是呈现当前视图的视图在下面是不可见的。事实上,它模糊了黑色背景。但是,如果视图的呈现方式是 animated,则仅在动画期间以适当的模糊背景呈现视图。

我也尝试过这样展示视图,但无济于事:

self.presentViewController(vc, animated: true, completion:{vc.makeBackgroundBlurry()})

如何解决这个问题,使模态呈现的视图在呈现期间和完成时都具有模糊的背景?

the view which presented the current view is not visible underneath

这是您应该期待的。当您呈现一个视图控制器时,呈现视图控制器的视图默认情况下会消失。呈现的视图控制器的视图背后没有任何内容。那是正常的。因此,您会在半透明视图控制器的视图后面看到 window 的黑色。

然而,在 iOS 8 中,您可以通过使用 .OverFullScreen 作为呈现视图控制器的模态呈现样式来防止呈现视图控制器的视图消失:

 vc.modalPresentationStyle = .OverFullScreen