Duplicate, clone or copy UIView
Sure. The documentation has a good example of how to achieve that; it's for UITableViewCell
, but is a good approach to use here as well.
Depending on the complexity of your view, you might want to make it a custom view class, and give it its own IBOutlet
properties for whatever subviews it has; in that case, you'd set the “Class Identity” of the view in Interface Builder to that class. Then your view controller could access those views on any given XIB-loaded view via, for instance, myLoadedView.someLabel
, rather than having to use, e.g., [myLoadedView viewWithTag:3]
as suggested by the aforelinked documentation.
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.
UIView duplicate
The easiest (but probably not the fastest) way is to archive it into an NSData object and unarchive it right away.
NSData *tempArchive = [NSKeyedArchiver archivedDataWithRootObject:myView];
UIView *myViewDuplicate = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchive];
How to make copy UIView and save accessibilityId programmatically?
This appears to be an Apple bug. You should really report this on Apple Bug Reporter.
That said, you can work around the issue as follows:
extension UIView {
func copyView<T: UIView>() -> T {
let copy = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self)) as! T
copy.accessibilityIdentifier = accessibilityIdentifier
return copy
}
}
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.
How to duplicate an UIView designed in a storyboard?
Cheap way, give the view a tag.
then UIView *view = [UIView viewWithTag:tag];
and then go ahead and copy it, add it to the view.
Or try this:
id copyOfView = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:originalView]];
Bear in mind that not everything necessarily gets archived and unarchived correctly (custom fonts, layer settings like corner radius) and IBOutlets should be steered clear of as they don't get updated on the cloned objects.
Are UIViews duplicated when adding a subview again in Swift?
I guess we are talking about UIViews and not Layers here?!
In this case usernameLabel is just added once even if you call addSubview multiple times.
To avoid any ambiguity here some code:
let baseView = UIView()
let dateLabel = UILabel()
let usernameLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
baseView.tag = 1
usernameLabel.tag = 2
dateLabel.tag = 3
self.view.addSubview(self.baseView)
self.baseView.addSubview(self.usernameLabel)
self.baseView.addSubview(self.dateLabel)
self.baseView.addSubview(self.usernameLabel)
self.traverseViewHierarchy(view: self.baseView, level: 0)
}
private func traverseViewHierarchy(view: UIView, level: Int) {
for _ in 0...level {
print (" | ", terminator: "")
}
print ("view: \(view.tag)")
for view in view.subviews {
self.traverseViewHierarchy(view: view, level: level + 1)
}
}
This add some tags to the mention views and gives it out in the console:
| view: 1
| | view: 3
| | view: 2
As you can see usernameLabel is just added once to the view hierarchy.
CollectionView duplicate inside UIView
the simplest way to fix this will be to replace
print(genrView)
with:
collectionView.reloadData()
in your
var genrView : [String] = []{
didSet {
collectionView.reloadData()
}
}
Related Topics
Cgcolorgetcomponents() Not Returning the Correct Values for Black and for White
Using the C API for Imagemagick (On Iphone) to Convert to Monochrome
How to Prevent a Remote Notification from Being Displayed
Error Sending Archive to App Store Connect
How to Use Multiple Segues with One Uitableviewdelegate
Ios7 - Device Unique Identifier
How to Open a Link to a PDF with Wkwebview
Uitapgesturerecognizer on Uiimageview Within Uitablevlewcell Not Getting Called
Why Does "Try Catch" in Objective-C Cause Memory Leak
Draggesture on a VStack with Lot of Buttons, How to Detect When the Drag Is Inside a Button
How to Segue with Data from One Tab to Another Tab Properly
Create Custom International Keyboard for Iphone
Are Storyboards Going to Work on iOS 4
Using Private API to Read Wifi Rssi Value