Remove BlurView in swift
Asked Answered
T

2

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?

Terminal answered 3/11, 2015 at 16:51 Comment(0)
L
15

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()
    }
}
Lusterware answered 3/11, 2015 at 16:59 Comment(1)
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 UIVisualEffectViewFootslog
P
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()
}
Progression answered 19/3, 2020 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.