视图上的模糊背景不一致地工作

Mic*_*ola 1 background viewcontroller ios swift uiblureffect

使用以下代码使视图模糊的背景不能始终如一地工作.

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))
}
Run Code Online (Sandbox Code Playgroud)

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

@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)

    }
Run Code Online (Sandbox Code Playgroud)

它很少会按预期工作,在背景上产生光线模糊效果; 但是,大多数情况下背景模糊不清,但呈现当前视图的视图在下方不可见.事实上,它模糊了黑色背景.但是,如果视图的呈现是animated,则仅在动画期间向视图呈现适当的模糊背景.

我也试过提出这样的观点无济于事:

self.presentViewController(vc, animated: true, completion:{vc.makeBackgroundBlurry()})
Run Code Online (Sandbox Code Playgroud)

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

mat*_*att 6

显示当前视图的视图在下方不可见

这是你应该期待的.当您呈现视图控制器时,默认情况下,呈现视图控制器的视图就会消失.显示的视图控制器视图后面没有任何内容.这很正常.因此,您将看到半透明视图控制器视图后面的窗口的黑色.

但是,在iOS 8中,您可以通过使用.OverFullScreen呈现的视图控制器的模式演示样式来防止呈现的视图控制器的视图消失:

 vc.modalPresentationStyle = .OverFullScreen
Run Code Online (Sandbox Code Playgroud)