What is the correct way to remove a subview from a

2019-03-09 05:22发布

问题:

I have a parent UIView with a number of subviews. Periodically I need to remove a subview and completely remove it from the system. What is the correct way to do this? I tried this:

UIView *v = [self.containerView viewWithTag:[n integerValue]];

[v removeFromSuperview];

and got a bizarre result. Previously present UIViews disappeared as well. What's going on?

回答1:

Try this:

UIView *v = [self.containerView viewWithTag:[n integerValue]];
v.hidden = YES;
[self.containerView bringSubviewToFront:v];
[v removeFromSuperview];

Another thing I just noticed from the UIView class document - see the last sentence:

removeFromSuperview Unlinks the receiver from its superview and its window, and removes it from the responder chain.

  • (void)removeFromSuperview

Discussion 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.

Never invoke this method while displaying.

UPDATE: It is now 2014 and removing a subview without hiding it works perfectly fine. The original poster's code should work as-is:

UIView *v = [self.containerView viewWithTag:[n integerValue]];
[v removeFromSuperview];

This will remove v and any views it has attached to it as subviews, leaving behind containerView and any siblings of v.



回答2:

To remove all subviews from your view:

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

If you want to remove some specific view only then:

for(UIView *subview in [view subviews]) {
  if([subview isKindOfClass:[UIButton class]]) {
     [subview removeFromSuperview];
 } else {
     // Do nothing - not a UIButton or subclass instance
 }
}

You can also delete sub views by tag value:

for(UIView *subview in [view subviews]) {
    if(subview.tag==/*your subview tag value here*/) {
        [subview removeFromSuperview];

    } else {
        // Do nothing - not a UIButton or subclass instance
    }
}


回答3:

Swift 3.0:

let viewToRemove = mySuperView.viewWithTag(myTag)
viewToRemove?.removeFromSuperview()


回答4:

That's the right general idea. those other UIViews that disappear, what's their relationship to this UIView? Are they subviews of this view? Are they dealloc'd in the dealloc method of the view you're removing?

Are you sure your Tags are unique?

Sujal



回答5:

Are they just disappearing from the display, or disappearing from the display and the view hierarchy? What does the debugger show you?



回答6:

Is it possible that cell.contentView has the same tag as the subview you want to remove? according to the documentation viewWithTag removes:

The view in the receiver’s hierarchy that matches tag. The receiver is included in the search.

If this is the case then you may be inadvertently removing cell.contentView from the cell. If n is zero and your cell's contentview has no tag set to it, it would default to 0 and cause that to happen.



回答7:

Swift 4: extend UIView

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

or

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