使用以下代码使视图的背景变得模糊并不能始终如一地工作。
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()})
如何解决这个问题,以使模态呈现的视图在呈现期间和完成时都具有模糊的背景?