Michael Voccola
Michael Voccola

Reputation: 1835

Blurred Background on View not Working Consistently

Using the following code to make the background of a view blurry is 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))
}

The background blur is being applied to a view controller that is being presented modally like so:

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

    }

Infrequently, it will work as desired, creating a light blur effect on the background; however, most of the time the background is blurred but the view which presented the current view is not visible underneath. In fact, it is blurring a black background. If, however, the presentation of the view is animated, then the view is presented with the proper blurred background only during the animation.

I have also tried presenting the view like so to no avail:

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

How can this be resolved so that the modally presented view has a blurred background both during it's presentation and on it's completion?

Upvotes: 1

Views: 1556

Answers (1)

matt
matt

Reputation: 535167

the view which presented the current view is not visible underneath

That is what you should expect. When you a present a view controller, the presenting view controller's view, by default, simply goes away. There is nothing behind the presented view controller's view. That is normal. Thus you are seeing the black of the window behind your translucent view controller's view.

In iOS 8, however, you can prevent the presented view controller's view from going away by using .OverFullScreen as the presented view controller's modal presentation style:

 vc.modalPresentationStyle = .OverFullScreen

Upvotes: 6

Related Questions