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

前端 未结 5 1797
无人及你
无人及你 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条回答
  •  梦如初夏
    2021-01-31 10:07

    Swift version of Sandy's solution.

    if #available(iOS 9.2, *) {
        passwordTextField.secureTextEntry = !passwordTextField.secureTextEntry
    }
    else {
        let wasFirstResponder = passwordTextField.isFirstResponder()
        if wasFirstResponder {
            passwordTextField.resignFirstResponder()
        }
    
        passwordTextField.secureTextEntry = !passwordTextField.secureTextEntry
        if wasFirstResponder {
            passwordTextField.becomeFirstResponder()
        }
    }
    

提交回复
热议问题