Remove all subviews?

前端 未结 15 1700

When my app gets back to its root view controller, in the viewDidAppear: method I need to remove all subviews.

How can I do this?

15条回答
  •  粉色の甜心
    2020-11-28 00:48

    In Swift you can use a functional approach like this:

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

    As a comparison, the imperative approach would look like this:

    for subview in view.subviews {
        subview.removeFromSuperview()
    }
    

    These code snippets only work in iOS / tvOS though, things are a little different on macOS.

提交回复
热议问题