ios 复制、克隆或复制 UIView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5004055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:53:31  来源:igfitidea点击:

Duplicate, clone or copy UIView

xcodeios

提问by Thizzer

I have a UIView that is linked to a UIViewController via the Interface Builder. Is it possible to duplicate, clone or copy this view so that I can use it more than once?

我有一个 UIView,它通过 Interface Builder 链接到 UIViewController。是否可以复制、克隆或复制此视图以便我可以多次使用它?

采纳答案by Noah Witherspoon

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.

当然。文档有一个很好的例子来说明如何实现这一点;它用于UITableViewCell,但也是在这里使用的好方法。

Depending on the complexity of your view, you might want to make it a custom view class, and give it its own IBOutletproperties 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.

根据视图的复杂性,您可能希望将其设为自定义视图类,并为其拥有的IBOutlet任何子视图赋予其自己的属性;在这种情况下,您可以将 Interface Builder 中视图的“类标识”设置为该类。然后您的视图控制器可以通过例如,访问任何给定的 XIB 加载视图上的这些视图myLoadedView.someLabel,而不必使用,例如,[myLoadedView viewWithTag:3]如上述文档所建议的那样。

回答by osxdirk

The following category might not be particularly efficient, but worked for me in one project:

以下类别可能不是特别有效,但在一个项目中对我有用:

@implementation UIView (OPCloning)

- (id) clone {
    NSData *archivedViewData = [NSKeyedArchiver archivedDataWithRootObject: self];
    id clone = [NSKeyedUnarchiver unarchiveObjectWithData:archivedViewData];
    return clone;
}

@end

I'd not implement -copy or -copyWithZone: as Apple might do so in the future. Note, that not all views implement archiving to the same extent. You'll definitely need to implement the NSCoding methods for custom properties of your NSView subclasses to be cloned (will turn to nil in the cloned view, otherwise). Still easier than writing custom cloning code.

我不会实现 -copy 或 -copyWithZone: 因为 Apple 将来可能会这样做。请注意,并非所有视图都在相同程度上实现归档。您肯定需要为要克隆的 NSView 子类的自定义属性实现 NSCoding 方法(否则将在克隆视图中变为 nil)。仍然比编写自定义克隆代码更容易。

回答by Lei Zhang

Here is a new method you can use: Use UIView's method:

这是您可以使用的新方法:使用 UIView 的方法:

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates

This is the fastest way to draw a view. Available in iOS 7.

这是绘制视图的最快方法。在 iOS 7 中可用。

回答by Sour LeangChhean

You can try with Swift 3.0.1 below:

您可以尝试使用下面的 Swift 3.0.1:

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