Remove the view from one view and add it to another

I try to find all my views that are in nib and add them to my content.

This is what I have. It successfully removes the view from self.view, but does not add it to self.contentView

for (UIView *view in self.view.subviews) { if (view.tag != 666) { [view removeFromSuperview]; [self.contentView addSubview:view]; } } 

Any help would be appreciated.

+4
source share
7 answers

The problem is in your code, when you call removeFromSuperview , the view will be released by the parent view. No need to call removeFromSuperview , just add it as a subtitle of another view to remove it from the current parent.

Therefore use:

 for (UIView *view in self.view.subviews) { if (view.tag != 666) { [self.contentView addSubview:view]; } } 

According to the UIView class reference :

addSubview

Adds a view to the end of the list of recipients for the view.

- (void)addSubview:(UIView *)view

Parameters

View

 The view to be added. This view is retained by the receiver. After being added, this view appears on top of any other subviews. 

Discussion

This method saves the view and sets its next responder to the receiver, which is its new overview.

Views can only have one view. If the view already has a supervisor and this view is not the recipient, this method deletes the previous one before making the recipient a new supervisor .

+4
source

I'm not sure if this will work, but try switching to [view removeFromSuperview]; and [self.contentView addSubview:view]; . This is because according to the UIView class reference , removeFromSuperview forces the supervisor to release the view.

Hope this helps!

0
source

Check for errors. You better print frames of these views (self.view, self.contentView). and make them different colors. Then you can see the errors. Good luck

0
source

contentView does not belong to the UIVew property. It belongs to the UITableViewCell property. It returns a representation of the contents of the cell object.

0
source
  for (UIView *v in innerMainView.subviews) { [v removeFromSuperview]; } [innerMainView addSubview:StandardView]; 
0
source

You can set the specified view as a property or add a view to the content view before removing it from the super view. I'm not sure when you delete a view, if the save rate of views is reduced to 0, it will trigger a superview to delete it.

0
source

When you remove your view from superView , it will be release from memory . Therefore, in your case, you need to do the following:

 UINib *infoNib = [UINib nibWithNibName:@"YourXIBFileName" bundle:nil]; NSArray *topLevelObjects = [infoNib instantiateWithOwner:self options:nil]; UIView *infoView = [topLevelObjects objectAtIndex:0]; for (UIView *view in infoView.subviews) { [self.contentView addSubview:view]; } } 
0
source

All Articles