7

I'm having issues with toggling the secureEntry property on UITextField. When the property is toggled, the characters are resized, but the cursor stays in the wrong place:

insecure entry secure entry

HighFlyingFantasy
  • 3,789
  • 2
  • 26
  • 38
  • possible duplicate of [UITextField secureTextEntry - works going from YES to NO, but changing back to YES has no effect](http://stackoverflow.com/questions/6710019/uitextfield-securetextentry-works-going-from-yes-to-no-but-changing-back-to-y) – Andrei Mar 25 '14 at 16:07
  • @Andrei totally did the trick. you're the man – HighFlyingFantasy Mar 25 '14 at 16:09

4 Answers4

4

Here is my workaround:

textField.secureTextEntry = YES;

UITextRange *textRange = textField.selectedTextRange;
textField.selectedTextRange = nil;
textField.selectedTextRange = textRange;

Disable and then Enable the UITextField also help, but it will suddenly change my soft keyboard from one to another

0x5e
  • 41
  • 3
4

After setting textField secureEntry property NO, you need to call becomeFirstResponder method its works for me...

Try this

 textField.secureTextEntry = NO;
 if (textField.isFirstResponder){
    [textField becomeFirstResponder];
 }
keshav vishwkarma
  • 1,832
  • 13
  • 20
3

Both are working for Swift 4

self.isSecureTextEntry = false

if textField.isFirstResponder {
    textField.becomeFirstResponder()
    textField.layoutIfNeeded() 
}

I had to add textField.layoutIfNeeded() that it's working for Swift or use

    let textRange = self.selectedTextRange
    self.selectedTextRange = nil;
    self.selectedTextRange = textRange
kuzdu
  • 7,124
  • 1
  • 51
  • 69
1

For Swift 4

@objc func tapOnLeftIcon(gesture: UITapGestureRecognizer) {
        let textRange = textField.selectedTextRange

        textField.isSecureTextEntry = !textField.isSecureTextEntry
        textField.selectedTextRange = nil
        textField.selectedTextRange = textRange
    }
Grifas
  • 217
  • 4
  • 15