title: Change right icon image UITextField
body: <p>I am trying to change between rightView UIImages that are added as a subView to my UITextfield. When a user taps the icon it should show or hide the inserted password and change the UIImage depending on the state.</p>
  
  <p>How am I able to edit the code below to achieve this?</p>
  
  <p>Code: </p>
  
  <pre><code>// 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
          }
      }
  </code></pre>
  
  <p>Thanks a lot!</p>