我在 iOS 16 上使用“isSecureTextEntry”来解决这个问题,https://stackoverflow.com/a/76390952/22598343
extension UIView {
func makeSecure() {
DispatchQueue.main.async {
let field = UITextField()
field.isSecureTextEntry = true
self.addSubview(field)
field.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
field.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
self.layer.superlayer?.addSublayer(field.layer)
field.layer.sublayers?.first?.addSublayer(self.layer)
}
}
}
它现在看起来无法在 iOS 17 上运行。还有其他解决办法吗?
我尝试过 DRM 方式,但性能非常糟糕。
来自:https://developer.apple.com/forums/thread/736112
改变:
field.layer.sublayers?.first?.addSublayer(self.layer)
至:
field.layer.sublayers?.last?.addSublayer(self.layer)