Search code examples
iosswiftviewuiblureffectuivisualeffectview

Remove subview from other method


I initialize UIVisualEffectView from one method and need to remove it with other method.

func showBlur() {
    let blurEffect: UIBlurEffect = UIBlurEffect(style: .Light)
    let blurView = UIVisualEffectView(effect: blurEffect)
    // ...
}
func destroyBlur {
    // i can’t destroy via remove from superview it here, blurView was initialized in showBlur()
}

I’ve tried to initiate blurView not in method but in class… there is problems with init() in UIViewController.

I’ve tried to find that view in view.subViews, but i’ve got no luck.

I’ve tried to set tag to UIView but can’t find that tag lately in subView list.


Solution

  • You can create property to store UIVisualEffectView on the beginning od the class:

    var blurView: UIVisualEffectView!
    

    and in showBlur() function, sto the effect in this variable:

    let blurEffect: UIBlurEffect = UIBlurEffect(style: .Light)
    blurView = UIVisualEffectView(effect: blurEffect)
    

    and in destroyBlur() call:

    blurView.removeFromSuperview()