I am having a weird issue in which my UITextField
which holds a secure entry is always getting cleared when I try to edit it. I added 3 characters
to the field, goes to another field and comes back, the cursor is in 4th character
position, but when I try to add another character, the whole text in the field gets cleared by the new character. I have 'Clears when editing begins' unchecked in the nib. So what would be the issue? If I remove the secure entry property, everything is working fine, so, is this the property of Secure entry textfields
? Is there any way to prevent this behaviour?
Set,
textField.clearsOnBeginEditing = NO;
Note: This won't work if secureTextEntry = YES. It seems, by default, iOS clears the text of secure entry text fields before editing, no matter clearsOnBeginEditing is YES or NO.
If you don't want the field to clear, even when secureTextEntry = YES, use:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *updatedString = [textField.text stringByReplacingCharactersInRange:range withString:string];
textField.text = updatedString;
return NO;
}
I encountered a similar issue when adding show/hide password text functionality to a sign-up view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With