3

I come across many post about disabling long press in UItextView, but that same process is not working for UITextField and UISearchBar.

Is there any way to disable Long press on UITextField and UISearchBar so I can avoid Magnifying glass on long press?

I have already checked "Disable Magnifying Glass in UITextField" but solution given there is not actually disabling magnifying glass. Its just not allowing cursory to Move between text. but still displaying Magnifying glass.

I want to disable Long Press - to avoid Problem which I am facing with magnifying glass. I want to disable it now. will enable it again when I will able to fix this.

Community
  • 1
  • 1
PlusInfosys
  • 3,416
  • 1
  • 19
  • 33

4 Answers4

2

You can try following for disabling only built in long press gesture

for (UIGestureRecognizer *recognizer in textView.gestureRecognizers) {
  if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
    recognizer.enabled = NO;
  }
}

or

delegate method for all :-

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return NO;
}
Tanvi Jain
  • 917
  • 2
  • 7
  • 19
0

This combination in a custom class is the only thing I've been able to get to work for me, and on the first long press it still sometimes makes the magnifying glass appear:

//swift 4.2
override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer)
{

    if gestureRecognizer.isKind(of: UILongPressGestureRecognizer.self)
    {
        print(gestureRecognizer.name.unwrappedDebugString)
        gestureRecognizer.isEnabled = false
    }
    return super.addGestureRecognizer(gestureRecognizer)
}
override func becomeFirstResponder() -> Bool
{
    for recognizer in self.gestureRecognizers ?? []
    {
        if (recognizer is UILongPressGestureRecognizer)
        {
            recognizer.isEnabled = false
        }
    }
    return super.becomeFirstResponder()
}
A. L. Strine
  • 611
  • 1
  • 7
  • 23
-1

@Neeraj answer in Swift 4 :

class CustomTextView: UITextView {

   override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {

     if gestureRecognizer.isKind(of: UILongPressGestureRecognizer.self) {
              gestureRecognizer.isEnabled = false
     }
    return super.addGestureRecognizer(gestureRecognizer)
   }
}
yo2bh
  • 1,356
  • 1
  • 14
  • 26
  • In iOS 12, if the text field is the first responder, the magnifying glass will still appear and the disappear quickly after the long press is finished with this code, interrupting touchUpInside. – A. L. Strine Jul 04 '19 at 21:45
-2

One good and clean way to do this is you can create a custom class for the UITextField and in the custom class you can override the long-press gesture which is causing the magnification view appear. This custom class can be used in all places where we don't want to show the magnification view for test field. Just use below class in place of UITextField class

class CustomTextView: UITextView {

    override func addGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
        if (gestureRecognizer.isKindOfClass(UILongPressGestureRecognizer)) {
            gestureRecognizer.enabled = false;
        }
        super.addGestureRecognizer(gestureRecognizer)
        return
    }
}
Neeraj Pathak
  • 311
  • 2
  • 18