Mihai Fratu Mihai Fratu - 4 months ago 20
iOS Question

How to duplicate an UIView designed in a storyboard?

So I have this view that I design in a storyboard view controller and I want to reuse that design more than once from code. Is that possible to do in XCode 5? If so can you please show me the right direction?


Cheap way, give the view a tag.

then UIView *view = [UIView viewWithTag:tag];

and then go ahead and copy it, add it to the view.

Or try this:

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

Bear in mind that not everything necessarily gets archived and unarchived correctly (custom fonts, layer settings like corner radius) and IBOutlets should be steered clear of as they don't get updated on the cloned objects.