如何以编程方式复制UIView并保存accessibilityId?

时间:2018-05-16 15:05:01

标签: ios swift cocoa-touch

我用:

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

但是我的副本正在丢失可访问性标识符。

copyView.accessibilityIdentifier == nil

如何保存?

1 个答案:

答案 0 :(得分:0)

这似乎是一个Apple bug。你应该在Apple Bug Reporter上报告这个。

也就是说,您可以解决以下问题:

extension UIView {
    func copyView<T: UIView>() -> T {
        let copy = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self)) as! T
        copy.accessibilityIdentifier = accessibilityIdentifier
        return copy
    }
}