0

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)
}
koen
  • 5,383
  • 7
  • 50
  • 89
GDog
  • 117
  • 1
  • 8
  • What is a `UIBlurView` ? – koen Nov 17 '19 at 15:17
  • - UIVisualEffectView – GDog Nov 17 '19 at 15:19
  • Works for me. I don't use IB, so I created my blur view through code like in the answer. (I also didn't `removeFromSuperview`, as the **will** remove any views in `blurView.contentView`.) All I can think is when using IB (and/or the automatic `weak`), maybe `viewDidLoad` is too early in the view controller lifecycle for this? Try the *latest* override you can, `viewDidLayoutSubviews` and see what happens. –  Nov 17 '19 at 17:04
  • Yes, it worked, I have accidentally put another blur view on top of the existing one -> that's why it didn't work, but thanks for the help :) – GDog Nov 18 '19 at 10:50

1 Answers1

1

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

    }
J Erasmus
  • 64
  • 1
  • 4