Detect Focus Change for UITextField

后端 未结 4 1783
臣服心动
臣服心动 2021-02-01 02:20

I\'m trying to set the animation for the view to move up when keyboard is hiding and appearing for the text fields and I got it to work perfectly fine, but when the focus moves

相关标签:
4条回答
  • 2021-02-01 02:32
    -(BOOL)textFieldShouldBeginEditing:(UITextField*)textField {
    if (textField.tag == 1) { //first textField tag
        //textField 1
    }
    else {
       //textField 2
    }
    }
    
    0 讨论(0)
  • Use UITextFieldDelegate

    and

    func textFieldDidBeginEditing(textField: UITextField) {
            println("did")
            if textField.tag == 1{
                self.txtFullName.layer.borderColor = UIColor.blueColor().CGColor
            }
        }
    
    0 讨论(0)
  • 2021-02-01 02:40

    Use the UITextField delegate methods .. its better on your case than the keyboard methods .. when textField got focus the - (void)textFieldDidBeginEditing:(UITextField *)textField; will be fired .. and when it lost focus - (void)textFieldDidEndEditing:(UITextField *)textField; will be fired.

    0 讨论(0)
  • 2021-02-01 02:45

    In swift 4:

    1. Implement UITextFieldDelegate into your ViewController class
    2. Add the Outlets for the textFields you want to detect focus on
    3. Implement function textFieldDidBeginEditing (in case you want to be notified on that specific action, see Apple's documentation) and fill it with your code
    4. On viewDidLoad() assign your ViewController class as the delegate for the textField you previously added in step 2

    It must look similar to:

    class ViewController: UIViewController, UITextFieldDelegate {
        @IBOutlet weak var yourTextField: UITextField!
    
        func textFieldDidBeginEditing(_ textField: UITextField) {
            // Your code here
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            yourTextField.delegate = self
        }
    
        ...
    
    }
    
    0 讨论(0)
提交回复
热议问题