swiftxcodeuiblureffect

UIVisualEffectView won't disappear after effect = nil


I have a blur effect on my ViewController, but by calling blurView.effect = nil, the blur won't disappear, even though if i call print(blurView.effect) it prints out "nil". What can I do?

@IBOutlet weak var blurView: UIVisualEffectView!
var effect =UIVisualEffect!

override func viewDidLoad() {
    super.viewDidLoad()

    effect = blurView.effect
    blurView.effect = nil
    print(blurView.effect)
}

Solution

  • I'm pretty sure the answer you are looking for is :

    blurView.removeFromSuperview()
    instead of
    blurView.effect = nil

    Using that will remove the view and everything in it from the superview.

    I hope that helps.

    I didn't test the above answer, but if I were you I would rather apply it this way :

        override func viewDidLoad() {
            super.viewDidLoad()
    
    
            let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
            let blurEffectView = UIVisualEffectView(effect: blurEffect)
            blurEffectView.frame = self.view.bounds
            blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            self.view.addSubview(blurEffectView)
    
            blurEffectView.removeFromSuperview()
    
        }