Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all subviews?

Tags:

ios

subview

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?

like image 380
Ian Vink Avatar asked Jan 28 '10 16:01

Ian Vink


People also ask

How do I delete all arranged Subviews?

Managing Subviews To remove an arranged subview that you no longer want around, you need to call removeFromSuperview() on it. The stack view will automatically remove it from the arranged subview list.

How to remove a subview from an array of subviews?

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.

How to add subview in iOS Swift?

How To Add, Insert, Move, Remove Sub View In iOS Swift 1 Manage Swift View And Subview Example Demo. First let us look at this example in a video as below. ... 2 Manage Swift Subview Functions Introduction. parentViewObject.addSubview ( subView : UIView ) : Add a subview to parent view. ... 3 Manage Swift Subview Example Source Code.

How to remove all subviews in a UIViewController?

extension UIView { func lf_removeAllSubviews() { for view in self.subviews { view.removeFromSuperview() } } } So that you can use self.view.lf_removeAllSubviews in a UIViewController.

Are UIView and NSView the same in Swift?

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 1 Swift 4 gives an error: Cannot assign to property: 'subviews' is a get-only property.


2 Answers

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.

like image 86
e.James Avatar answered Oct 08 '22 12:10

e.James


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]; } 
like image 22
Matthew McGoogan Avatar answered Oct 08 '22 12:10

Matthew McGoogan