How to remove all subviews?

IosSubview

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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];
}

Solution 3 - Ios

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.

Solution 4 - Ios

If you want to remove all the subviews on your UIView (here yourView), then write this code at your button click:

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

Solution 5 - Ios

This does only apply to OSX since in iOS a copy of the array is kept

When removing all the subviews, it is a good idea to start deleting at the end of the array and keep deleting until you reach the beginning. This can be accomplished with this two lines of code:

for (int i=mySuperView.subviews.count-1; i>=0; i--)
        [[mySuperView.subviews objectAtIndex:i] removeFromSuperview];

SWIFT 1.2

for var i=mySuperView.subviews.count-1; i>=0; i-- {
    mySuperView.subviews[i].removeFromSuperview();
}

or (less efficient, but more readable)

for subview in mySuperView.subviews.reverse() {
    subview.removeFromSuperview()
}

NOTE

You should NOT remove the subviews in normal order, since it may cause a crash if a UIView instance is deleted before the removeFromSuperview message has been sent to all objects of the array. (Obviously, deleting the last element would not cause a crash)

Therefore, the code

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

should NOT be used.

Quote from Apple documentation about makeObjectsPerformSelector:

> Sends to each object in the array the message identified by a given > selector, starting with the first object and continuing through the > array to the last object.

(which would be the wrong direction for this purpose)

Solution 6 - Ios

Try this way swift 2.0

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

Solution 7 - Ios

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

Solution 8 - Ios

Use the Following code to remove all subviews.

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

Solution 9 - Ios

Using Swift UIView extension:

extension UIView {
    func removeAllSubviews() {
        for subview in subviews {
            subview.removeFromSuperview()
        }
    }
}

Solution 10 - Ios

In objective-C, go ahead and create a category method off of the UIView class.

- (void)removeAllSubviews
{
    for (UIView *subview in self.subviews)
        [subview removeFromSuperview];
}

Solution 11 - Ios

In order to remove all subviews Syntax :

- (void)makeObjectsPerformSelector:(SEL)aSelector;

Usage :

[self.View.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

This method is present in NSArray.h file and uses NSArray(NSExtendedArray) interface

Solution 12 - Ios

If you're using Swift, it's as simple as:

subviews.map { $0.removeFromSuperview }

It's similar in philosophy to the makeObjectsPerformSelector approach, however with a little more type safety.

Solution 13 - Ios

For ios6 using autolayout I had to add a little bit of code to remove the constraints too.

NSMutableArray * constraints_to_remove = [ @[] mutableCopy] ;
for( NSLayoutConstraint * constraint in tagview.constraints) {
    if( [tagview.subviews containsObject:constraint.firstItem] ||
       [tagview.subviews containsObject:constraint.secondItem] ) {
        [constraints_to_remove addObject:constraint];
    }
}
[tagview removeConstraints:constraints_to_remove];

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

I'm sure theres a neater way to do this, but it worked for me. In my case I could not use a direct [tagview removeConstraints:tagview.constraints] as there were constraints set in XCode that were getting cleared.

Solution 14 - Ios

In monotouch / xamarin.ios this worked for me:

SomeParentUiView.Subviews.All(x => x.RemoveFromSuperview);

Solution 15 - Ios

In order to remove all subviews from superviews:

NSArray *oSubView = [self subviews];
for(int iCount = 0; iCount < [oSubView count]; iCount++)
{
    id object = [oSubView objectAtIndex:iCount];
    [object removeFromSuperview];
    iCount--;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIan VinkView Question on Stackoverflow
Solution 1 - Iose.JamesView Answer on Stackoverflow
Solution 2 - IosMatthew McGooganView Answer on Stackoverflow
Solution 3 - IosJeehutView Answer on Stackoverflow
Solution 4 - IosMohd RahibView Answer on Stackoverflow
Solution 5 - IosDanielView Answer on Stackoverflow
Solution 6 - IosWilliam HuView Answer on Stackoverflow
Solution 7 - IosMAGiGOView Answer on Stackoverflow
Solution 8 - IosShahzaib MaqboolView Answer on Stackoverflow
Solution 9 - IosmixelView Answer on Stackoverflow
Solution 10 - IosRicksterView Answer on Stackoverflow
Solution 11 - IosJayprakash DubeyView Answer on Stackoverflow
Solution 12 - IoslmirosevicView Answer on Stackoverflow
Solution 13 - IosMichael AndersonView Answer on Stackoverflow
Solution 14 - IosDaniele D.View Answer on Stackoverflow
Solution 15 - IosPravinView Answer on Stackoverflow