Home » Ios » Can UIView be copied?

Can UIView be copied?

Posted by: admin November 30, 2017 Leave a comment

Questions:

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?

Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

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()

Questions:
Answers:

for swift3.0.1:

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

Questions:
Answers:

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.

Questions:
Answers:

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