how to remove all subviews using swfit

how to remove all subviews using swfit?

i try

let backgroundView = sender.view as UIView?

if let view = backgroundView{

UIView.animate(withDuration: 0.3,

animations:{ () in

},

completion: {(finished:Bool) in

view.alpha = 0

view.superview?.removeFromSuperview()

if view.subviews.count > 0

{

view.subviews.forEach({ $0.removeFromSuperview()})

}

})

}


but only the view removed.

all the subviews cannot be removed....

I think the problem is that after line 8, view's superview has been removed, so you cannot access its subviews anymore.


let backgroundView = sender.view as UIView?
        if let view = backgroundView{
            UIView.animate(withDuration: 0.3,
                           animations:{ () in
            },
                           completion: {(finished:Bool) in
                            view.alpha = 0
                            view.superview?.removeFromSuperview()
                            if view.subviews.count > 0
                            {
                                view.subviews.forEach({ $0.removeFromSuperview()})
                            }
                         
            })
        }



Try


let backgroundView = sender.view as UIView?
        if let view = backgroundView{
            UIView.animate(withDuration: 0.3,
                           animations:{ () in
            },
                           completion: {(finished:Bool) in
                            view.alpha = 0
                            if view.subviews.count > 0
                            {
                                view.subviews.forEach({ $0.removeFromSuperview()})
                            }
                            view.superview?.removeFromSuperview()

            })
        }
how to remove all subviews using swfit
 
 
Q