Can UIView be copied?

Forrest picture Forrest · Dec 13, 2010 · Viewed 46k times · Source

Simply using this way:

UIView* view2 = [view1 copy]; // view1 existed

This will cause simulator can not launch this app.

Try retain,

UIView* view2 = [view1 retain]; // view1 existed
// modify view2 frame etc

Any modifications to view2 will apply to view1, I understand that view2 share same memory with view1.

Why can't UIView be copied? What is the reason?

Answer

j2emanue picture j2emanue · Dec 7, 2012

this might work for you ... archive the view and then unarchive it right after. This should give you a deep copy of a view:

id copyOfView = 
[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:originalView]];