Can UIView be copied?

IosObjective CUiviewCopy

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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]];

Solution 2 - Ios

Your app probably crashes with something like:

 [UIView copyWithZone:]: unrecognized selector sent to instance 0x1c6280

The reason is that UIView does not implement the copying protocol, and therefore there is no copyWithZone selector in UIView.

Solution 3 - Ios

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: UIView>() -> T {
            return NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self)) as! T
       }
    }

Example usage:

let sourceView = UIView()
let copiedView = sourceView.copyView()

Solution 4 - Ios

for swift3.0.1:

extension UIView{
 func copyView() -> AnyObject{
    return NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self))! as AnyObject
 }
}

Solution 5 - Ios

UIView doesn't implement the NSCoping protocol, see the declaration in UIView.h:

@interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusEnvironment>

So, if we want to have a copy like method, we need to implement the NSCoping protocol in a category or so.

Solution 6 - Ios

You can make method something like this:

-(UILabel*)copyLabelFrom:(UILabel*)label{
//add whatever needs to be copied
UILabel *newLabel = [[UILabel alloc]initWithFrame:label.frame];
newLabel.backgroundColor = label.backgroundColor;
newLabel.textColor = label.textColor;
newLabel.textAlignment = label.textAlignment;
newLabel.text = label.text;
newLabel.font = label.font;

return [newLabel autorelease];

}

Then you can set your ivar to the return value and retain it like so:

myLabel = [[self copyLabelFrom:myOtherLabel] retain];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionForrestView Question on Stackoverflow
Solution 1 - Iosj2emanueView Answer on Stackoverflow
Solution 2 - IosEngin KurutepeView Answer on Stackoverflow
Solution 3 - IosIvan PorkolabView Answer on Stackoverflow
Solution 4 - IosSour LeangChheanView Answer on Stackoverflow
Solution 5 - IosHongchaoZhangView Answer on Stackoverflow
Solution 6 - IosssjView Answer on Stackoverflow