UITextfield .isSecureTextEntry 破坏了应用程序

问题描述

所以基本上我在 viewDidLoad 中这样做:

self.passwordTextfield.delegate = self
self.passwordTextfield.textContentType = .password
self.passwordTextfield.isSecureTextEntry = true

一切正常,直到我按 Enter 并且应用程序冻结。在模拟器上我没有收到错误 - 如果我在实际手机上产生错误,它会冻结,一分钟后 Xcode 显示错误消息:

“来自调试器的消息:由于内存问题而终止”

如果我设置 self.passwordTextfield.isSecureTextEntry = false 一切正常。

我使用 textfieldShouldReturn(_textField: UITextField) 在按下 Enter 键时移除键盘

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

我像这样子类化了 UITextfield:

@IBDesignable
class Textfield: UITextField {

let padding: UIEdgeInsets = UIEdgeInsets(top: 0,left: 15,bottom: 0,right: 15)
let height: CGFloat = 60
let cornerRadius: CGFloat = 15

override init(frame: CGRect) {
    super.init(frame: frame)
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
}

override func layoutSubviews() {
    super.layoutSubviews()
    
    self.setupTextfield()
}

private func setupTextfield() {
    // height
    self.frame.size.height = self.height
    // style
    self.styleTextfield()
    // correction
    self.autocorrectionType = .no
}

private func styleTextfield() {
    // font
    self.font = UIFont.systemFont(ofSize: 17)
    // color
    self.textColor = Constants.Colors.dark_blue.withAlphaComponent(0.6)
    // alignment
    self.contentVerticalAlignment = .center
    // default background
    self.backgroundColor = isEditing ? Constants.Colors.light_white : Constants.Colors.light_grey
    // border
    self.borderStyle = .none
    // corners
    self.layer.cornerRadius = self.cornerRadius
    // shadow
    self.layer.shadowColor = UIColor.black.cgColor
    self.layer.shadowOpacity = 0.1
    self.layer.shadowOffset = CGSize(width: 0,height: 3)
}

override func textRect(forBounds bounds: CGRect) -> CGRect {
    return bounds.inset(by: padding)
}

override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
    return bounds.inset(by: padding)
}

override open func editingRect(forBounds bounds: CGRect) -> CGRect {
    return bounds.inset(by: padding)
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)