MR Mido MR Mido - 4 months ago 19x
iOS Question

How to switch between hide and view password in ios?

Is there a clever way to let the user switch between hide and view password in an ios/iphone UITextField where the user can enter a password and would have the option of either hide it or view it.
thanks in advance.


Simply setting

.secureTextEntry = YES

wont work I think due to a bug (or a feature), if the textField has focus.

Use something like this to make it work also if the textField is currently firstResponder

-(void) toggleTextFieldSecureEntry: (UITextField*) textField {
    BOOL isFirstResponder = textField.isFirstResponder; //store whether textfield is firstResponder

    if (isFirstResponder) [textField resignFirstResponder]; //resign first responder if needed, so that setting the attribute to YES works
    textField.secureTextEntry = !textField.secureTextEntry; //change the secureText attribute to opposite
    if (isFirstResponder) [self.textField becomeFirstResponder]; //give the field focus again, if it was first responder initially