SwiftyJD SwiftyJD - 5 months ago 175
iOS Question

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

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.

secure text entry

I know you can check mark the "secure text entry" box in the attributes inspector but how to do it so it toggles whenever the icon is pressed?

Answer

Use this code,

iconClick is bool variable, or you need other condition check it,

var iconClick : Bool!

viewDidLoad method:

    iconClick = true

eye Action method:

@IBAction func iconAction(sender: AnyObject) {
        if(iconClick == true) {
            passwordTF.secureTextEntry = false
            iconClick = false
        } else {
            passwordTF.secureTextEntry = true
            iconClick = true
        }

    }

hope its helpful