Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView duplicate

I need to duplicate a view, with all it's subviews.

Does anyone knows how to do it?

like image 539
mxg Avatar asked Jan 25 '10 09:01

mxg


2 Answers

The easiest (but probably not the fastest) way is to archive it into an NSData object and unarchive it right away.

NSData *tempArchive = [NSKeyedArchiver archivedDataWithRootObject:myView];
UIView *myViewDuplicate = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchive];
like image 62
Ole Begemann Avatar answered Nov 15 '22 19:11

Ole Begemann


To archive any uiimage object simply convert them to an imagerep of one kind or another (PNG for instance) and archive that NSData object, then in the unarchive you restore the uiimage with imageWithData:

like image 21
April Avatar answered Nov 15 '22 20:11

April