This answer shows how to do the same thing in Swift as the accepted answer does in Objective-C.
let tempArchive = NSKeyedArchiver.archivedDataWithRootObject(myView)
let myViewDuplicate = NSKeyedUnarchiver.unarchiveObjectWithData(tempArchive) as! UIView
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:
You can drag your custom view outside the root view of the viewController.
In the implementation of the custom view class, override the enconde/decode methods. Realize the IBOutlets and IBActions connections will not be archived in memory.
Swift version
This answer shows how to do the same thing in Swift as the accepted answer does in Objective-C.
My full answer with more notes is here.
Here is a new method you can use: Use UIView's method:
This is the fastest way to draw a view. Available in iOS 7.
The easiest (but probably not the fastest) way is to archive it into an NSData object and unarchive it right away.
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:
You can drag your custom view outside the root view of the viewController. In the implementation of the custom view class, override the enconde/decode methods. Realize the IBOutlets and IBActions connections will not be archived in memory.
MyCustomView.h:
Sample of reusing the custom view in ViewController.m:
One can easily copy a UIView by archiving and unarchiving process.
But this ignores UIImageView, so for UIImageView do it by this way,
Refrence from here.