MR Mido MR Mido - 1 year ago 96
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.

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download