UITextField has trailing whitespace after secureTextEntry toggle

后端 未结 18 1359
逝去的感伤
逝去的感伤 2020-12-29 19:27

I have a button that toggles between Show/Hide mode (i.e. toggles a UITextField between secureTextEntry NO and YES). The purpose of which is to allow the user to see the pa

18条回答
  •  庸人自扰
    2020-12-29 20:11

    When we change a textfield.secureTextEntry property, the caret position is not updated. To fix this, the code below used to work before IOS 8:

    pwdTextField.text  = pwdTextField.text
    

    Now it doesn't. It seems IOS 8 detects the new value equals old value and does nothing. So to make it work again we have to actually change the value. Here is the swift version that works for me.

    let str = pwdTextField.text
    pwdTextField.text = str + " "
    pwdTextField.text = str
    

提交回复
热议问题