更改右图标图像UITextField

时间:2016-12-05 18:44:11

标签: ios swift xcode uitextfield

我正在尝试在作为子视图添加到我的UITextfield的rightView UIImages之间进行更改。当用户点击图标时,它应显示或隐藏插入的密码,并根据状态更改UIImage。

我如何编辑下面的代码来实现这个目标?

代码:

// Show eye icon to show the password
    func textFieldDidChange() {
        if passwordField.text != "" {
            let leftImageView = UIImageView()
            leftImageView.image = UIImage(named: "view_password")
            leftImageView.contentMode = .scaleAspectFit

            let leftView = UIView()
            leftView.addSubview(leftImageView)

            leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 30)
            leftImageView.frame = CGRect(x: 10, y: 10, width: 20, height: 12)

            passwordField.rightViewMode = .whileEditing
            passwordField.rightView = leftView

            let tapgesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.togglePasswordSecurity))
            leftImageView.isUserInteractionEnabled = true
            leftImageView.addGestureRecognizer(tapgesture)

        }else{
            passwordField.rightViewMode = .never
        }
    }


    func togglePasswordSecurity(imageView: UIImageView) {

        if passwordField.isSecureTextEntry == false {

            // change left UIImage to hide_password
            passwordField.isSecureTextEntry = true
        }else{

            // change left UIImage to view_password
            passwordField.isSecureTextEntry = false
        }
    }

非常感谢!

2 个答案:

答案 0 :(得分:0)

您是否尝试为每张图片添加标签? 例如:

let leftImageView = UIImageView()
leftImageView.image = UIImage(named: "view_password")
leftImageView.contentMode = .scaleAspectFit
leftImageView.tag = 1

然后,要更改该图像,您可以写:

let myImage = UIView(withTag: 1) as! UIImageView
myImage.image = ...

编辑: 要在两个图像之间切换,只需更改此项:     func togglePasswordSecurity(imageView:UIImageView){

    if passwordField.isSecureTextEntry == false {

        let myImage = UIView(withTag: 1) as! UIImageView
        myImage.image = YOUR_IMAGE_1
        // change left UIImage to hide_password
        passwordField.isSecureTextEntry = true
    }else{

        let myImage = UIView(withTag: 1) as! UIImageView
        myImage.image = YOUR_IMAGE_2
        // change left UIImage to view_password
        passwordField.isSecureTextEntry = false
    }
}

答案 1 :(得分:0)

<强>解决方案:

// Show eye icon to show the password will be triggered once there is a change in data in the UITextField
    func textFieldDidChange() {
        if passwordField.text != "" {
            let leftImageView = UIImageView()
            leftImageView.image = passwordImage
            leftImageView.contentMode = .scaleAspectFit
            leftImageView.tag = 1

            let leftView = UIView()
            leftView.addSubview(leftImageView)

            leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 30)
            leftImageView.frame = CGRect(x: 10, y: 10, width: 20, height: 12)

            passwordField.rightViewMode = .whileEditing
            passwordField.rightView = leftView

            let tapgesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.togglePasswordSecurity))
            leftImageView.isUserInteractionEnabled = true
            leftImageView.addGestureRecognizer(tapgesture)

        }else{
            passwordField.rightViewMode = .never
        }
    }


    func togglePasswordSecurity() {

        if passwordField.isSecureTextEntry == false {

            passwordImage = UIImage(named: "view_password")
            textFieldDidChange()

            // change left UIImage to hide_password
            passwordField.isSecureTextEntry = true
        }else{

            passwordImage = UIImage(named: "heart")
            self.textFieldDidChange()
            // change left UIImage to view_password
            passwordField.isSecureTextEntry = false
        }
    }