What is the best way to remove all subviews from you self.view?

前端 未结 5 585
孤城傲影
孤城傲影 2020-12-12 12:07

I was thinking maybe something like this might work:

    for (UIView* b in self.view.subviews)
    {
       [b removeFromSuperview];
    }

5条回答
  •  孤街浪徒
    2020-12-12 13:09

    For Swift 4+.You can make a extension to UIView. Call it whenever necessary.

    extension UIView {
        func removeAllSubviews() {
            subviews.forEach { $0.removeFromSuperview() }
        }
    }
    

提交回复
热议问题