使用以下代码使 View 的背景变得模糊无法始终如一地工作。
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))
}
背景模糊被应用于模态呈现的 View Controller ,如下所示:
@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)
}
偶尔,它会按预期工作,在背景上产生光模糊效果;然而,大多数时候背景是模糊的,但是呈现当前 View 的 View 在下面是不可见的。事实上,它模糊了黑色背景。但是,如果 View 的呈现是动画
,则 View 仅在动画期间以适当的模糊背景呈现。
我也试过这样展示 View ,但无济于事:
self.presentViewController(vc, animated: true, completion:{vc.makeBackgroundBlurry()})
如何解决这个问题,以便模态呈现的 View 在呈现期间和完成时都具有模糊的背景?
最佳答案
the view which presented the current view is not visible underneath
这是您应该期待的。当您呈现一个 View Controller 时,呈现 View Controller 的 View 默认情况下会消失。呈现的 View Controller 的 View 背后没有任何内容。那是正常的。因此,您会在半透明 View Controller 的 View 后面看到黑色的窗口。
但是,在 iOS 8 中,您可以通过使用 .OverFullScreen
作为呈现的 View Controller 的模态呈现样式来防止呈现的 View Controller 的 View 消失:
vc.modalPresentationStyle = .OverFullScreen
关于ios - View 上的模糊背景无法始终如一地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30159787/