How to move cursor from one text field to another automatically in swift ios programmatically?

后端 未结 13 2124
抹茶落季
抹茶落季 2020-12-05 08:11
    func textFieldDidBeginEditing(textField: UITextField) {
    scrlView.setContentOffset(CGPointMake(0, textField.frame.origin.y-70), animated: true)


    if(textF         


        
13条回答
  •  伪装坚强ぢ
    2020-12-05 09:03

    I have tried many codes and finally this worked for me in Swift 3.0 Latest [March 2017]

    The "ViewController" class should inherited the "UITextFieldDelegate" for making this code working.

    class ViewController: UIViewController,UITextFieldDelegate 
    

    Add the Text field with the Proper Tag nuber and this tag number is used to take the control to appropriate text field based on incremental tag number assigned to it.

    override func viewDidLoad() {
    
     userNameTextField.delegate = self
    
            userNameTextField.tag = 0
    
            userNameTextField.returnKeyType = UIReturnKeyType.next
    
            passwordTextField.delegate = self
    
            passwordTextField.tag = 1
    
    
            passwordTextField.returnKeyType = UIReturnKeyType.go
    
    }
    

    In the above code, the "returnKeyType = UIReturnKeyType.next" where will make the Key pad return key to display as "Next" you also have other options as "Join/Go" etc, based on your application change the values.

    This "textFieldShouldReturn" is a method of UITextFieldDelegate controlled and here we have next field selection based on the Tag value incrementation

    func textFieldShouldReturn(_ textField: UITextField) -> Bool
    
        {
    
            if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
    
                nextField.becomeFirstResponder()
    
            } else {
    
                textField.resignFirstResponder()
    
                return true;
    
            }
    
            return false
    
        }
    

提交回复
热议问题