How to disable UITextField editing but still accept touch?

后端 未结 15 2296
误落风尘
误落风尘 2020-11-29 19:55

I\'m making a UITextField that has a UIPickerView as inputView. Its all good, except that I can edit by copy, paste, cut and select te

15条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 20:25

    This workaround works. Put a transparent UIView above the text field and implement the following code:

    - (void)viewDidLoad
    {
     [super viewDidLoad];
    
       UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]             initWithTarget:self action:@selector(longPress)];
    [transparentView addGestureRecognizer:press];
     [press release];
      press = nil;
    }
    
    -(void)longPress
    {
       txtField.userInteractionEnabled = NO;
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
       txtField.userInteractionEnabled = YES;
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
      [txtField becomeFirstResponder];
    }
    

提交回复
热议问题