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)
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…