What is the best way to remove all subheadings from you self.view?

I thought there might be something like this:

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

I want to remove every kind of subview. UIImages, Buttons, Text Fields, etc.

+76
ios objective-c iphone uikit
Aug 09 '12 at 17:55
source share
4 answers
 [self.view.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)]; 

It is identical to your version, but a little shorter.

+220
09 Aug '12 at 17:59
source share
— -
 self.view.subviews.forEach({ $0.removeFromSuperview() }) 

Identical version in Swift.

+11
Feb 22 '16 at 2:52
source share

Swift:

 extension UIView { func removeAllSubviews() { for subview in subviews { subview.removeFromSuperview() } } } 
+6
Sep 08 '15 at 16:47
source share

You can use this as

 //adding an object to the view view.addSubView(UIButton()) // you can remove any UIControls you have added with this code view.subviews.forEach { (item) in item.removeFromSuperview() } 

view is the view from which you want to remove everything. you just delete each subview element by doing forEach

+1
Sep 09 '16 at 1:13
source share



All Articles