简体   繁体   中英

remove UIView from superView

I have two UIViews, UIView1 and UIView2. UIView2 is being added as a subview to UIView1, what I would like to know is that if I call

[UIView1 removeFromSuperview];

dose that also apply the removeFromSuperView on UIView2? or do I specifically have to call that on the UIView2 as well...

any help would be appreciated

UIView---->UIView1------>UIView2

Suppose you want to remove UIView2 on UIView1, just you can write like

[UIView2 removeFromSuperView].

Suppose you want to remove UIView1 on UIView, just you can write like

[UIView1 removeFromSuperView].

In this situation, UIView2 is also removed. Because when you remove any view, all subviews of view will be removed.

There is no need to call [UIView2 removeFromSuperView] because when you remove UIView1 it will also remove its all subviews.

Hope this helps.

If you consider that all UIButtons, UILabels and other controls are inherited from UIView, and you do not explicitly remove them from your view, it becomes clear that removing a parent view from superview also results in removal of all child subviews.

Therefore, you do not need to call [UIView2 removeFromSuperview]

you did UIView2 is added as a subview of UIView1. So that if you call [UIView1 removeFromSuperview]; means What ever your added in it and also UIView1 removed from superview. so dont need to call [UIView2 removeFromSuperView];

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

if the receiver's superview is not nil, the superview releases the receiver. If you plan to reuse a view, be sure to retain it before calling this method and release it again later as appropriate.

Here [UIView2 removeFromSuperView] will remove UIView2 from UIView1

Also [UIView1 removeFromSuperView] will remove all its subViews from its superView(May be window or another view)

Better you have to create mainview like

UIView * main=[[[UIView alloc] initWithFrame:CGRectMake(20, 20, 20, 20)]autorelease];
[self.view addsubview main];
[main addsubview UIView1];
[main addsubview UIView2];

then you call

[UIView1 removeFromSuperview];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM