0

Rather than individually removeFromSubview for each thing I want to remove, I'd like to know if there's something which removes everything from the superview.

Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75

3 Answers3

0

I am using category method for this

@interface UIView (Additions)
- (void)removeAllSubviews
@end

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

So you can call

[self.superview removeAllSubviews];

to remove everything from superview.

Avt
  • 16,927
  • 4
  • 52
  • 72
0
for(UIView *view in self.superview.subviews)
{
    [view removeFromSuperview];
}

Be careful with this though, it can remove Apple built-ins that you don't know are there and thereby change the way the view interacts with the user.

Putz1103
  • 6,211
  • 1
  • 18
  • 25
0

One liner

[viewYouWantToRemoveFrom.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
Paul.s
  • 38,494
  • 5
  • 70
  • 88