7

I have a UIView that contains buttons and labels. When these buttons are pressed, this UIView will become blur using the code below.

@IBOutlet weak var blurView: UIView!
var blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
var blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = blurView.bounds
blurView.addSubview(blurEffectView)

However, I want to remove the blur effect later on. What is the code for removing the blurred UIView?

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
Clarence
  • 1,951
  • 2
  • 34
  • 49

2 Answers2

14

It's hard to know exactly what is going on in your code, as you've clearly posted a cut up version (the last 4 lines are part of a method somewhere, presumably).

You could do something like this to remove all UIVisualEffectView subviews from your blurView:

for subview in blurView.subviews {
    if subview is UIVisualEffectView {
        subview.removeFromSuperview()
    }
}
Charles A.
  • 10,685
  • 1
  • 42
  • 39
  • 4
    you can add a tag to the view and use viewWithTag: so you don't have to iterate and identify the correct view just in case you have multiple UIVisualEffectView – n3wb May 07 '17 at 16:28
0
func addBlurEffect(view: UIView, style: UIBlurEffect.Style) {
    view.backgroundColor = UIColor.clear

    let blurEffect = UIBlurEffect(style: style)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = view.bounds
    blurEffectView.tag = 9
    view.insertSubview(blurEffectView, at: 0)
}

func removeBlurEffect(view: UIView){
    view.viewWithTag(9)?.removeFromSuperview()
}
J A S K I E R
  • 1,976
  • 3
  • 24
  • 42