Restrict to UITextField cursor should only focus on end of text

I need to restrict to UITextField text cursor should only focus on end of text every time. Ex- if user entered OTP in textField and again he want to replace digit from text field when he tap to UITextField then cursor should focus on end of text only not beginning of text. I added below code

func textFieldDidBeginEditing(_ textField: UITextField){

DispatchQueue.main.async{

let position = textField.endOfDocument textField.selectedTextRange = textField.textRange(from: position, to: position)

}

}

It is working On first time textfield selection. After once cursor focussed in UITextField that time it is moving to Start position if user tap on start position.

It is working On first time textfield selection. After once cursor focussed in UITextField that time it is moving to Start position if user tap on start position.

Problem is that textFieldDidBeginEditing is called when you start editing, not when you ```

func textFieldDidBeginEditing(_ textField: UITextField){
  DispatchQueue.main.async{
    let position = textField.endOfDocument textField.selectedTextRange = textField.textRange(from: position, to: position)
  }
}

You should implement it in an IBAction connected to Touched Down or Touched Up event of the UITextField.

Restrict to UITextField cursor should only focus on end of text
 
 
Q