Randy Randy - 4 months ago 11
iOS Question

How to disable hold and scroll on a UITextField

I have a

UITextField
and like every self-respecting
UITextField
, it is possible to hold on it and scroll it by moving the cursor ( also called caret ).
I'd like to know if there is a way to disable this feature ?

Answer

I based my answer using this post: http://stackoverflow.com/a/16586297/6611229 but I made changes to solve an issue. You can create a subclass of UITextField and add the following methods to the class:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
   self.selectedTextRange = [self textRangeFromPosition:self.endOfDocument
                                            toPosition:self.endOfDocument];

   return NO;
}

- (CGRect)caretRectForPosition:(UITextPosition*)position
{
   return [super caretRectForPosition:self.endOfDocument];
}