UITextField secureTextEntry - works going from YES to NO, but changing back to YES has no effect

前端 未结 5 1796
无人及你
无人及你 2021-01-31 09:13

The above says it all- I have a UITextField set to secure, but want to give users the option to make it not secure (so they can see for sure what they typed if they are in a pri

5条回答
  •  -上瘾入骨i
    2021-01-31 10:14

    It must be input-focus issue: when focused, UITextField can change only ON->OFF.
    Try next trick to switch OFF->ON:

    textField.enabled = NO;
    textField.secureTextEntry = YES;
    textField.enabled = YES;
    [textField becomeFirstResponder];
    

    EDIT (dhoerl): In iOS 9.3, I found this works but there is a problem. If you enter say 3 characters in plain view, then switch to secure text, then type a character, the 3 pre-existing characters disappear. I tried all kinds of trick to clear, then reset the text without success. I finally tried just playing with the control by cutting and pasting - pasting into the newly-switched-to-secure-mode worked great. So I emulated it in code, and now it all works fine - no need to play with the responder either. Here is what I finally ended up with:

        if textview should go into secure mode and the textfield is not empty {
            textField.text = ""
            textField.secureTextEntry = true
    
            UIPasteboard.generalPasteboard().string = password
            textField.paste(self)
            UIPasteboard.generalPasteboard().string = ""
        }
    

    I'd prefer to not have to do this paste, but if you want it seamless this is the only way I can find to do it.

提交回复
热议问题