Can UIView be copied?
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.
Create a copy of a UIView in Swift
You can't arbitrarily copy an object. Only objects that implement the NSCopying
protocol can be copied.
However, there is a workaround: Since UIView
s can be serialized to disk (e.g. to load from a XIB), you could use NSKeyedArchiver
and NSKeyedUnarchiver
to create a serialized NSData
describing your view, then de-serialize that again to get an independent but identical object.
Clone or Copy UIViewController or UIView
Use -
NSData *tempArchiveView = [NSKeyedArchiver archivedDataWithRootObject:self.view];
UIView *viewOfSelf = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchiveView];
Similarly -
NSData *tempArchiveViewController = [NSKeyedArchiver archivedDataWithRootObject:self];
UIViewController *controller = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchiveViewController];
Copy of UiView and all subviews..Copied UIButton cannot be pressed
Since this is all done in viewDidLoad
, I assume that the view you want to copy is the same every time.
Your code does not work likely because NSKeyedArchiver
does not archive the button's target and selector pairs.
You can create a method that gives a new UIView
instead:
func createPart() -> UIView {
let part = UIView()
part.translatesAutoresizingMaskIntoConstraints = false
part.layer.cornerRadius = 4
part.layer.masksToBounds = true
part.backgroundColor = .random()
// The part below is copied from your viewDidLoad method
// Include only those lines that create the part view.
// I might have put more than you need. Check twice
let button = UIButton(frame: CGRect(x: 270, y: 200, width: 80, height: 40))
let partLabel1 = UILabel(frame: CGRect(x:10, y: 10, width: 300, height: 50))
let partLabel2 = UILabel(frame: CGRect(x:10, y: 50, width: 300, height: 50))
partLabel1.text = "This should sit within part use :)"
partLabel1.textColor = .white
partLabel2.text = "This should also sit within part use :)"
partLabel2.textColor = .white
part.addSubview(button)
part.addSubview(partLabel1)
part.addSubview(partLabel2)
part.bringSubviewToFront(button)
part.bringSubviewToFront(partUse3Label1)
part.layer.zPosition = -1
button.setTitle("Issue", for: .normal)
button.backgroundColor = .orange
button.leadingAnchor.constraint(equalTo: part.leadingAnchor).isActive = true
button.trailingAnchor.constraint(equalTo: part.trailingAnchor).isActive = true
button.bottomAnchor.constraint(equalTo: part.bottomAnchor).isActive = true
button.topAnchor.constraint(equalTo: part.topAnchor).isActive = true
button.heightAnchor.constraint(equalToConstant: 40).isActive = true
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.isUserInteractionEnabled = true
part.bringSubviewToFront(button)
partLabel1.leadingAnchor.constraint(equalTo: part.leadingAnchor).isActive = true
partLabel1.trailingAnchor.constraint(equalTo: part.trailingAnchor).isActive = true
partLabel2.leadingAnchor.constraint(equalTo: part.leadingAnchor).isActive = true
partLabel2.trailingAnchor.constraint(equalTo: part.trailingAnchor).isActive = true
part.layoutIfNeeded()
return part
}
And then in viewDidLoad
, you should remove the lines of code that help create the part
view, leaving only the code that creates the stack view and main content view. You should then call createPart
twice, and there you have 2 copies!
let part = createPart()
let copyOfPart = createPart()
Related Topics
Foreign Key Relationship Mapping with Restkit
Changing Vc Issue in Swift. How to Pass Data Between Views in Tab Bar Controller
What Is the '' Is or the ':' at the Equation When Coding in Swift
How to Log a Method's Execution Time Exactly in Milliseconds
How to Get Unique Id in iOS Device
No Avplayer Delegate? How to Track When Song Finished Playing? Objective C iPhone Development
How to Create Managedobjectcontext Using Swift 3 in Xcode 8
Change Tab Bar Item Selected Color in a Storyboard
Iphone: Hide Uitableview Search Bar by Default
iOS App Error - Can't Add Self as Subview
Programmatically Change Rootviewcontroller of Storyboard
iOS Multiple Columns in Uitableview
How to Add Unique Constraints for Some Fields in Core Data
Xcode 8 - Ib Designables - Failed to Render and Update Auto Layout Status, the Agent Crashed
How to Use Uiscrollview in Storyboard