221

I'm looking for a simple method to remove at once all subviews from a superview instead of removing them one by one.

//I'm trying something like this, but is not working
let theSubviews : Array = container_view.subviews
for (view : NSView) in theSubviews {
    view.removeFromSuperview(container_view)
}

What I am missing?

UPDATE

My app has a main container_view. I have to add different other views as subviews to container_view in order to provide a sort of navigation.

So, when clicking the button to "open" a particular page, I need to remove allsubviews and add the new one.

UPDATE 2 - A working solution (OS X)

I guess Apple fixed it.

Now it is more easy than ever, just call:

for view in containerView.subviews{
    view.removeFromSuperview()
}
Cristik
  • 30,989
  • 25
  • 91
  • 127
Alberto
  • 4,212
  • 5
  • 22
  • 36
  • 2
    I'd like to point out that @sulthan's answer, while buried with the rags, is the superior answer: http://stackoverflow.com/questions/24312760/swift-how-will-i-remove-all-the-subviews-of-a-view/24314054#24314054 – Christopher Swasey Jun 15 '15 at 21:47
  • @ChristopherSwasey Swift 4 gives an error: Cannot assign to property: 'subviews' is a get-only property. :( – William T. Mallard Feb 09 '18 at 19:33
  • 2
    @WilliamT.Mallard how many times does it have to be repeated that this method and question is about MacOS and not iOS? – Fogmeister Jun 20 '18 at 07:22

22 Answers22

411

EDIT: (thanks Jeremiah / Rollo)

By far the best way to do this in Swift for iOS is:

view.subviews.forEach({ $0.removeFromSuperview() }) // this gets things done
view.subviews.map({ $0.removeFromSuperview() }) // this returns modified array

^^ These features are fun!

let funTimes = ["Awesome","Crazy","WTF"]
extension String { 
    func readIt() {
        print(self)
    }
}

funTimes.forEach({ $0.readIt() })

//// END EDIT

Just do this:

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

Or if you are looking for a specific class

for view:CustomViewClass! in self.view.subviews {
        if view.isKindOfClass(CustomViewClass) {
            view.doClassThing()
        }
    }
Bseaborn
  • 4,347
  • 2
  • 14
  • 9
  • In Xcode 7.0 beta 6 this generates a warning: "result of call to 'map' is unused". I hope this gets fixed in the final version. – Ferschae Naej Sep 01 '15 at 06:21
  • I noticed that as well! I'll update the post once Xcode comes out of beta and the problem still persists. – Bseaborn Sep 01 '15 at 13:02
  • 10
    The warning, `result of call to 'map' is unused `, is not an error. `Array.map` in most languages will return the modified array. The equivalent method which doesn't return an array, would be `view.subviews.forEach`. –  Sep 03 '15 at 13:45
  • ```for view:CustomViewClass! in self.view.subviews where view.isKindOfClass(CustomViewClass) { view.doClassThing() }``` – iTSangar Oct 22 '15 at 04:31
  • I would argue that this is"By far the best way to do this in Swift" , see http://stackoverflow.com/a/24314054/1974224 – Cristik Jan 24 '16 at 10:18
  • @Cristik view.subviews is now a read-only property in Swift so the suggestion wouldn't work now. – Bseaborn Jan 26 '16 at 22:55
  • The [documentation](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/#//apple_ref/occ/instp/NSView/subviews) kinda says another thing. – Cristik Jan 26 '16 at 23:25
  • @Bseaborn I think you're confusing `NSView` and `UIView`, for `UIView` indeed, the `subviews` property is readonly. – Cristik Jan 26 '16 at 23:38
  • @Cristik I stand corrected! Thanks for pointing that out. Indeed I was confusing the two. I'll update my answer. – Bseaborn Jan 27 '16 at 00:46
46

For iOS/Swift, to get rid of all subviews I use:

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

to get rid of all subviews of a particular class (like UILabel) I use:

for v in view.subviews{
   if v is UILabel{
      v.removeFromSuperview()
   }
}
tonethar
  • 2,112
  • 26
  • 33
38

The code can be written simpler as following.

view.subviews.forEach { $0.removeFromSuperview() }
mishimay
  • 4,237
  • 1
  • 27
  • 23
24

This should be the simplest solution.

let container_view: NSView = ...
container_view.subviews = []

(see Remove all subviews? for other methods)


Note this is a MacOS question and this answer works only for MacOS. It does not work on iOS.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • This is definitely the simplest way... removeFromSuperview is automatically called for each view that is no longer in the subviews array. – Christopher Swasey Jun 15 '15 at 21:44
  • 2
    @datayeah They are not but there is a big difference between `NSView` (OS X) and `UIView` (iOS). – Sulthan Jan 26 '16 at 20:29
  • @Sulthan you're right. I came here for the Swift answer for UIView and did'nt read your whole code snippet ;) – dy_ Jan 26 '16 at 21:16
  • I'm more inclined to create an extension to implement `removeAllSubviews()` on `UIView`. Easier to remember. – Chen Li Yong Dec 14 '17 at 03:09
  • 1
    Swift 4 gives an error: Cannot assign to property: 'subviews' is a get-only property. :( – William T. Mallard Feb 09 '18 at 19:32
  • @WilliamT.Mallard This was originally an OS X question and this is OS X answer. – Sulthan Feb 09 '18 at 20:30
  • 2
    @AmrAngry Again, I repeat, this was always a Mac OS question, for `NSView`, not iOS and `UIView`. Only people don't care to read the question and tags therefore it got filled with iOS answers. – Sulthan Mar 08 '18 at 11:03
22

Swift 5

I create 2 different methods to remove subview. And it's much easier to use if we put them in extension

extension UIView {
    /// Remove all subview
    func removeAllSubviews() {
        subviews.forEach { $0.removeFromSuperview() }
    }

    /// Remove all subview with specific type
    func removeAllSubviews<T: UIView>(type: T.Type) {
        subviews
            .filter { $0.isMember(of: type) }
            .forEach { $0.removeFromSuperview() }
    }
}
Community
  • 1
  • 1
nahung89
  • 7,745
  • 3
  • 38
  • 40
14

Try this:

for view in container_view.subviews {
    view.removeFromSuperview()
}
MattL
  • 1,132
  • 10
  • 23
11

I don't know if you managed to resolve this but I have recently experienced a similar problem where the For loop left one view each time. I found this was because the self.subviews was mutated (maybe) when the removeFromSuperview() was called.

To fix this I did:

let subViews: Array = self.subviews.copy()
for (var subview: NSView!) in subViews
{
    subview.removeFromSuperview()
}

By doing .copy(), I could perform the removal of each subview while mutating the self.subviews array. This is because the copied array (subViews) contains all of the references to the objects and is not mutated.

EDIT: In your case I think you would use:

let theSubviews: Array = container_view.subviews.copy()
for (var view: NSView!) in theSubviews
{
    view.removeFromSuperview()
}
  • Hello, try the following although there is probably a much more elegant way to do this: `let subViewsArray: Array = (parentView.subviews as NSArray).copy() as Array` – Adam Richards Sep 03 '14 at 18:53
11

Extension for remove all subviews, it is quickly removed.

import Foundation
import UIKit

extension UIView {
    /// Remove allSubView in view
    func removeAllSubViews() {
        self.subviews.forEach({ $0.removeFromSuperview() })
    }

}
YanSte
  • 10,661
  • 3
  • 57
  • 53
8

Try this:

var subViews = parentView.subviews as Array<UIView>

      for someView in subViews
      {
          someView.removeFromSuperview()
      }

UPDATE: If you are feeling adventurous then you can make an extension on the UIView as shown below:

extension UIView
{
    func removeAllSubViews()
    {
       for subView :AnyObject in self.subviews
       {
            subView.removeFromSuperview()
       }
    }

}

And call it like this:

parentView.removeAllSubViews()
azamsharp
  • 19,710
  • 36
  • 144
  • 222
7

In xcodebeta6 this worked out.

    var subViews = self.parentView.subviews
    for subview in subViews as [UIView]   {
        subview.removeFromSuperview()
    }
mylegfeelsfunny
  • 507
  • 1
  • 8
  • 20
5

Swift 3

If you add a tag to your view you can remove a specific view.

for v in (view?.subviews)!
{
    if v.tag == 321
    {
         v.removeFromSuperview()
    }
 }
uplearned.com
  • 3,393
  • 5
  • 44
  • 59
4

I wrote this extension:

extension UIView {
    func lf_removeAllSubviews() {
        for view in self.subviews {
            view.removeFromSuperview()
        }
    }
}

So that you can use self.view.lf_removeAllSubviews in a UIViewController. I'll put this in the swift version of my https://github.com/superarts/LFramework later, when I have more experience in swift (1 day exp so far, and yes, for API I gave up underscore).

superarts.org
  • 7,009
  • 1
  • 58
  • 44
3

Your syntax is slightly off. Make sure you cast explicitly.

 let theSubviews : Array<NSView> = container_view.subviews as Array<NSView>
 for view in theSubviews {
     view.removeFromSuperview()
 }
Jack
  • 16,677
  • 8
  • 47
  • 51
  • @David Yes it will, I tried to keep it as close to FoxNos's initial code as possible. – Jack Jun 19 '14 at 19:03
3

you have to try this

func clearAllScrollSubView ()
{
    let theSubviews = itemsScrollView.subviews

    for (var view) in theSubviews
    {

        if view is UIView
        {
            view.removeFromSuperview()
        }

    }
}
Lochlan
  • 2,666
  • 3
  • 23
  • 25
  • That is UI not NS. Here we're talking about OS X. If that works even with it by replacing UI with NS, my bad, you were right :) – Alberto Nov 25 '14 at 16:55
2

Try this out , I tested this :

  let theSubviews = container_view.subviews
  for subview in theSubviews {
      subview.removeFromSuperview()
  }
Ezimet
  • 5,058
  • 4
  • 23
  • 29
2

For removing just subviews of a specific class - this was the only Swift code that worked for me in Xcode6.1.1. Assuming the only subviews you want to remove are of type UIButton...

for subView in nameofmysuperview.subviews {
    if subView.isKindOfClass(UIButton) {
        subView.removeFromSuperview()
    }
}
gammachill
  • 1,466
  • 1
  • 13
  • 14
2

For Swift 3

I did as following because just removing from superview did not erase the buttons from array.

    for k in 0..<buttons.count {

      buttons[k].removeFromSuperview()

    }


    buttons.removeAll()
Hope
  • 2,096
  • 3
  • 23
  • 40
2

For Swift 4.+

extension UIView {
     public func removeAllSubViews() {
          self.subviews.forEach({ $0.removeFromSuperview() })

}

i hope this is use full for you.

2

Here's a one-liner you've been looking for:

view.subviews.filter({$0.tag == 321}).forEach({$0.removeFromSuperview()})

Don't forget to Tag your view (someView.tag = 321) before you remove it with this code.

Codetard
  • 2,441
  • 28
  • 34
1

One-liner:

while view.subviews.count > 0  { (view.subviews[0] as? NSView)?.removeFromSuperview() } 
Sentry.co
  • 5,355
  • 43
  • 38
0

Here's another approach that allows you call the operation on any collection of UIView instances (or UIView subclasses). This makes it easy to insert things like filter after .subviews so you can be more selective, or to call this on other collections of UIViews.

extension Array where Element: UIView {
  func removeEachFromSuperview() {
    forEach {
      $0.removeFromSuperview()
    }
  }
}

Example usage:

myView.subviews.removeEachFromSuperview()

// or, for example:

myView.subivews.filter { $0 is UIImageView }.removeEachFromSuperview()

Alternatively you can accomplish the same thing with a UIView extension (though this can't be called on some arbitrary array of UIView instances):

extension UIView {
    func removeSubviews(predicate: ((UIView) -> Bool)? = nil) 
        subviews.filter(
            predicate ?? { _ in true }
        ).forEach {
            $0.removeFromSuperview()
        }
    }
}

Example usage:

myView.removeSubviews()
myView.removeSubviews { $0 is UIImageView }
shim
  • 9,289
  • 12
  • 69
  • 108
-3

did you try something like

for o : AnyObject in self.subviews {
     if let v = o as? NSView {
         v.removeFromSuperview()
     }
}
Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32