Categories
ios subview

How to remove all subviews?

330

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?

    573

    Edit: With thanks to cocoafan: This situation is muddled up by the fact that NSView and UIView handle things differently. For NSView (desktop Mac development only), you can simply use the following:

    [someNSView setSubviews:[NSArray array]];
    

    For UIView (iOS development only), you can safely use makeObjectsPerformSelector: because the subviews property will return a copy of the array of subviews:

    [[someUIView subviews]
     makeObjectsPerformSelector:@selector(removeFromSuperview)];
    

    Thank you to Tommy for pointing out that makeObjectsPerformSelector: appears to modify the subviews array while it is being enumerated (which it does for NSView, but not for UIView).

    Please see this SO question for more details.

    Note: Using either of these two methods will remove every view that your main view contains and release them, if they are not retained elsewhere. From Apple’s documentation on removeFromSuperview:

    If the receiver’s superview is not nil, this method releases the receiver. If you plan to reuse the view, be sure to retain it before calling this method and be sure to release it as appropriate when you are done with it or after adding it to another view hierarchy.

    9

    • 9

      Are you sure this is safe? It mutates the list while iterating it, and I’m unable to find a definitive statement in Apple’s documentation.

      – Tommy

      Mar 8, 2011 at 2:42

    • 8

      @Tommy: That is a good point. Some Googling turned up the answer: UIView returns a copy of the subviews mutable array, so this code just works. Completely different story on the desktop, where the same code will throw an exception. See stackoverflow.com/questions/4665179/…

      – e.James

      Mar 8, 2011 at 16:48

    • 3

      UIView does not respond to setSubviews:, does it?

      – cocoafan

      Mar 16, 2011 at 17:04

    • 4

      the Xamarin way : someUIView.Subviews.All(p => p.RemoveFromSuperview);

      Jun 2, 2015 at 19:14

    • 3

      @BenoitJadinon – won’t compile – you appear to mean abusing All to perform ForEach, so someUIView.Subviews.All( v => { v.RemoveFromSuperview(); return true; } );. IMHO cleaner to say what you mean: someUIView.Subviews.ToList().ForEach( v => v.RemoveFromSuperview() );.

      Jul 13, 2016 at 16:16


    174

    Get all the subviews from your root controller and send each a removeFromSuperview:

    NSArray *viewsToRemove = [self.view subviews];
    for (UIView *v in viewsToRemove) {
        [v removeFromSuperview];
    }
    

    5

    • +1 and thank you. I should have also used self.view as you have.

      – e.James

      Jan 28, 2010 at 16:32

    • 2

      why not!? for (UIView *v in [self.view subviews]) its easier

      – Frade

      Jun 18, 2014 at 11:31


    • 4

      @Frade It’s much clearer and more verbose the way he did it. Verbose and readability > saving keystrokes

      Aug 21, 2014 at 17:10

    • 34

      @taylorcressy You should have said “readability is more important than saving keystrokes” instead of “readability > saving keystrokes” and then your comment would be more readable. 🙂

      – arlomedia

      Jan 14, 2015 at 19:46


    • 1

      Let’s not forget about the fact that if [self.view subviews] performs any calculations under the hood, putting it directly in the for loop could cause those calculations to be performed over and over again. Declaring it before the loop ensures they are only performed once.

      Jul 17, 2018 at 14:38

    139

    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.

    4

    • 4

      (subviews as [UIView]).map { $0.removeFromSuperview() }

      – DeFrenZ

      Jan 2, 2015 at 13:13

    • 9

      it’s not functional since a function returns a value and this just discards the result of the .map. this is a pure side effect and is better handled like this: view.subviews.forEach() { $0.removeFromSuperview() }

      Nov 24, 2015 at 5:37

    • 1

      You are right, Martin, I agree with you. I just didn’t know there was a forEach() method on Arrays. When was it added or did I just oversee it? I’ve updated my answer!

      – Jeehut

      Nov 25, 2015 at 18:30

    • 1

      I am so lazy that even if I knew how to clear subviews, I still came here to copy/paste your snippet and put a +1

      – Vilmir

      Aug 4, 2019 at 9:10