How to toggle a UITextField secure text entry (hide password) in Swift?

前端 未结 24 2348
时光取名叫无心
时光取名叫无心 2021-01-30 02:00

I currently have a UITextfield with an eye icon in it that when pressed is supposed to toggle the secure text entry on and off.

I know you can che

24条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 02:52

    Try this code in swift 4, tried to make a reusable code within a controller. I have set different image for buttons in storyboard as shown in the link https://stackoverflow.com/a/47669422/8334818

    @IBAction func clickedShowPassword(_ sender: UIButton) {
            var textField :UITextField? = nil
            print("btn ",sender.isSelected.description)
            switch sender {
            case encryptOldPswdBtn:
                encryptOldPswdBtn.isSelected = !encryptOldPswdBtn.isSelected
                textField = oldPasswordTextField
    
            default:
              break
            }
    
            print("text ",textField?.isSecureTextEntry.description)
            textField?.isSecureTextEntry = !(textField?.isSecureTextEntry ?? false)
    
        }
    

提交回复
热议问题