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

前端 未结 24 2297
时光取名叫无心
时光取名叫无心 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:54

    Shortest!

    I think this is the shortest solution for secure entry as well as updating the picture of the button.

    @IBAction func toggleSecureEntry(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        textfieldPassword.isSecureTextEntry = !sender.isSelected
    }
    

    Assign the show/hide picture of the button according to the state selected /default , no need to create any variable or outlet.

    0 讨论(0)
  • 2021-01-30 02:55

    Use button with eye image
    and make buttonHandler method
    set Tag for button with value 1

    -(IBAction) buttonHandlerSecureText:(UIButton *)sender{
          if(sender.tag ==1){
                [self.textField setSecureTextEntry:NO];
                sender.tag = 2;
          }
          else{
                [self.textField setSecureTextEntry:YES];
                sender.tag = 1;
          }
    }
    
    0 讨论(0)
  • 2021-01-30 02:56

    For Objective c

    set image for RightButton In viewdidload Method

    [RightButton setImage:[UIImage imageNamed:@"iconEyesOpen"] forState:UIControlStateNormal];
    
        [RightButton setImage:[UIImage imageNamed:@"iconEyesClose"] forState:UIControlStateSelected];
    

    and then set action method for that RightButton

    -(IBAction)RightButton:(id)sender
    {
    
        if (_rightButton.selected)
        {
    
            _rightButton.selected = NO;
    
            _passwordText.secureTextEntry = YES;
    
    
            if (_passwordText.isFirstResponder) {
                [_passwordText resignFirstResponder];
                [_passwordText becomeFirstResponder];
            }
        }
        else
        {
    
          _rightButton.selected = YES;
    
            _passwordText.secureTextEntry = NO;
    
            if (_passwordText.isFirstResponder) {
                [_passwordText resignFirstResponder];
                [_passwordText becomeFirstResponder];
            }
    
        }
    }
    
    0 讨论(0)
  • 2021-01-30 02:56
    @IBAction func eye_toggle_clicked(sender: AnyObject)
    {
        if toggleBtn.tag == 0
        {
            passwordTxt.secureTextEntry=true
            toggleBtn.tag=1
        }
        else
        {
            passwordTxt.secureTextEntry=false
            toggleBtn.tag=0
        }
    }
    
    0 讨论(0)
  • 2021-01-30 02:57

    Use UITextFiled rightView to show toggle button

     var rightButton  = UIButton(type: .custom)
     rightButton.frame = CGRect(x:0, y:0, width:30, height:30)
     yourtextfield.rightViewMode = .always
     yourtextfield.rightView = rightButton
    
    0 讨论(0)
  • 2021-01-30 02:58

    If you need TextField with similar feature in multiple places its best to subclass the UITextField like follwing example -

    import UIKit
    
    class UIShowHideTextField: UITextField {
    
        let rightButton  = UIButton(type: .custom)
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            commonInit()
        }
    
        required override init(frame: CGRect) {
            super.init(frame: frame)
            commonInit()
        }
    
        func commonInit() {
            rightButton.setImage(UIImage(named: "password_show") , for: .normal)
            rightButton.addTarget(self, action: #selector(toggleShowHide), for: .touchUpInside)
            rightButton.frame = CGRect(x:0, y:0, width:30, height:30)
    
            rightViewMode = .always
            rightView = rightButton
            isSecureTextEntry = true
        }
    
        @objc
        func toggleShowHide(button: UIButton) {
            toggle()
        }
    
        func toggle() {
            isSecureTextEntry = !isSecureTextEntry
            if isSecureTextEntry {
                rightButton.setImage(UIImage(named: "password_show") , for: .normal)
            } else {
                rightButton.setImage(UIImage(named: "password_hide") , for: .normal)
            }
        }
    
    }
    

    After which you can use it in any ViewController,

    class ViewController: UIViewController {
    
        @IBOutlet var textField: UIShowHideTextField!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            textField.becomeFirstResponder()
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题