Simply using this way:
UIView* view2 = [view1 copy]; // view1 existed
This will cause simulator can not launch this app.
Try retai
You can make an UIView extension. In example swift snippet below, function copyView returns an AnyObject so you could copy any subclass of an UIView, ie UIImageView. If you want to copy only UIViews you can change the return type to UIView.
//MARK: - UIView Extensions
extension UIView
{
func copyView() -> T {
return NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self)) as! T
}
}
Example usage:
let sourceView = UIView()
let copiedView = sourceView.copyView()