微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UITextfield .isSecureTextEntry 破坏了应用程序

如何解决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 举报,一经查实,本站将立刻删除。