简体   繁体   English

如何在Swift中将一个视图复制到另一个视图

[英]how to copy one view to another view in Swift

I want to copy one UIView to another view without making it archive or unarchive. 我想将一个UIView复制到另一个视图而不进行存档或取消存档。 Please help me if you have any solution. 如果您有任何解决方案,请帮助我。

I tried with by making an extension of UIView as already available an answer on Stack over flow. 我尝试通过扩展UIView作为已经可用的Stack over flow的答案。 But its crashing when I pass the view with pattern Image Background color. 但是当我通过带有图案图像背景颜色的视图时,它崩溃了。

The code related to my comment below: 与我的评论相关的代码如下:

extension UIView
{
    func copyView() -> UIView?
    {
        return NSKeyedUnarchiver.unarchiveObjectWithData(NSKeyedArchiver.archivedDataWithRootObject(self)) as? UIView
    }
}

I've just tried this simple code in a Playground to check that the copy view works and it's not pointing the same view: 我刚刚在Playground中尝试了以下简单代码,以检查复制视图是否正常工作,并且未指向同一视图:

let originalView = UIView(frame: CGRectMake(0, 0, 100, 50));
originalView.backgroundColor = UIColor.redColor();
let originalLabel = UILabel(frame: originalView.frame);
originalLabel.text = "Hi";
originalLabel.backgroundColor = UIColor.whiteColor();
originalView.addSubview(originalLabel);

let copyView = originalView.copyView();
let copyLabel = copyView?.subviews[0] as! UILabel;

originalView.backgroundColor = UIColor.blackColor();
originalLabel.text = "Hola";

originalView.backgroundColor;   // Returns black
originalLabel.text;             // Returns "Hola"
copyView!.backgroundColor;      // Returns red
copyLabel.text;                 // Returns "Hi"

If the extension wouldn't work, both copyView and originalView would have same backgroundColor and the same would happen to the text of the labels. 如果扩展名不起作用,则copyVieworiginalView都将具有相同的backgroundColor并且标签的文本也将发生相同的情况。 So maybe there is the possibility that the problem is in other part. 因此,也许问题可能出在另一部分。

Original Post 原始帖子

   func copyView(viewforCopy: UIView) -> UIView {
        viewforCopy.hidden = false //The copy not works if is hidden, just prevention
        let viewCopy = viewforCopy.snapshotViewAfterScreenUpdates(true)
        viewforCopy.hidden = true
        return viewCopy
    }

Updated for Swift 4 为Swift 4更新

func copyView(viewforCopy: UIView) -> UIView {
    viewforCopy.isHidden = false //The copy not works if is hidden, just prevention
    let viewCopy = viewforCopy.snapshotView(afterScreenUpdates: true)
    viewforCopy.isHidden = true
    return viewCopy!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过使用 swift 5 的动态集合视图从一个视图 controller 移动到另一个视图 controller? - How to move from one view controller to another view controller though dynamic collection view using swift 5? 如何将值从一个视图控制器传递给另一个swift? - How to pass value from one view controller to another swift? 如何将UI ImageView从一个视图控制器传递到另一个? 迅捷3 - how to pass a UI ImageView from one view controller to another? swift 3 如何通过单击按钮在 Swift UI 中从一个视图导航到另一个视图 - How to navigate from one view to another in Swift UI on a click of button 如何在 Swift 中以编程方式从一个控制器导航到另一个视图控制器? - How to navigate from one controller to another View Controller programmatically in Swift? Swift-如何从一个视图控制器转移到另一个? - Swift - how to transfer from one view controller to another? swift:如何在另一个视图控制器中访问一个变量? - swift: How to take access of one variable in another view controller? 如何使用 Swift 从一个视图控制器导航到另一个视图控制器 - How to Navigate from one View Controller to another using Swift 将视图控制器从一个项目复制到另一个项目 - Copy view controller from one project to another 是否可以将约束从一个视图复制到另一个视图? - Is it possible to copy constraints from one view to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM