Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

swift - UITextfield .isSecureTextEntry breaks app

So basically I do this in viewDidLoad:

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

Everything works until I press Enter and the app freezes. On the simulator I don't get an error - if I produce the error on an actual phone, it freezes and after a minute Xcode displays this error message:

"Message from debugger: Terminated due to memory issue"

If I set self.passwordTextfield.isSecureTextEntry = false everything works fine.

I use the textfieldShouldReturn(_textField: UITextField) to remove the keyboard on pressing enter:

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

I subclassed UITextfield like so:

@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)
}

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...