0

I am trying to block any touch event on UITTextField. To do so, I am trying to place a UIView/Button/Label on top of UITexfield.

But on simulator, I can still click at any location inside text field. What is wrong with my code? How can I put UIButton/Lable on top of textfield so that user can not click anywhere inside text field.

Please note I still want Number Keyboard input in the textfield. I just want to disable clicks/cut/copy/paste/select etc.

self.textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 292, 50, 40)];
[self.textField becomeFirstResponder];
[self.textField setKeyboardType:UIKeyboardTypeNumberPad];

self.textField.delegate = self;
[self.view addSubview:self.textField];


UIButton *topViewonTextField = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
topViewonTextField.backgroundColor = [UIColor clearColor];
[topViewonTextField setEnabled:NO];
//topViewonTextField.layer.zPosition = 100;
[self.view addSubview:topViewonTextField];


#define MAIN_SCREEN                             [UIScreen mainScreen]
#define ScreenWidth                             [MAIN_SCREEN bounds].size.width
#define ScreenHeight                            [MAIN_SCREEN bounds].size.height
GJain
  • 5,025
  • 6
  • 48
  • 82
  • Where/how are you getting `ScreenWidth` and `ScreenHeight`? – Lyndsey Scott Nov 28 '14 at 23:07
  • Could you write out the specific code please? – Lyndsey Scott Nov 28 '14 at 23:09
  • OK, looks good. But I suspect the fact that your button is clear and not enabled makes your program essentially treat it as if it's hidden... If you check out the answers here you could see there may be some funky behavior with transparent views even if it's no longer documented: http://stackoverflow.com/questions/14170329/why-does-setting-a-uilabel-to-be-fully-transparent-lose-tap-gestures I don't think you're using the best strategy anyway though. Setting userInteractionEnabled to NO is much cleaner. – Lyndsey Scott Nov 28 '14 at 23:24

2 Answers2

0

If you want "block" a textField, tell to it:

 texField.userInteractionEnabled = NO;

Big explanations:

 // You create your textField, It can't receive events. By code or in Story

self.textField= [[UITextField alloc] initWithFrame:CGRectMake(10, 190, 80, 30)];
self.textField.placeholder = @"hola";
self.textField.userInteractionEnabled = NO;
self.textField.delegate = self;
[self.view addSubview:self.textField];

//Some Button or other event make the textField in editing mode.

UIButton *buttonToEdit = [[UIButton alloc] initWithFrame:CGRectMake(250, 250, 80, 80)];
[buttonToEdit setTitle:@"ActionToEdit" forState:UIControlStateNormal];
[buttonToEdit addTarget:self action:@selector(editMyTextField) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:buttonToEdit];

}
 -(void)editMyTextField
{
self.textField.userInteractionEnabled = YES;
[self.textField becomeFirstResponder];

}

// The las step (to you) is in the delegate, when the editing finish, go back to no interaction

#pragma mark - TextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField isEqual:self.textField]) {
    self.textField.userInteractionEnabled = NO;
    // more code here, validate...
}
}
Onik IV
  • 5,007
  • 2
  • 18
  • 23
  • @user2384694 You want it doesn't receive event, but It could be editabe – Onik IV Nov 28 '14 at 22:52
  • Your textField only can receive event when you are writing on it. And you want write on it, no?. It´s imposible stop events during the editing of a textField. If you want a display like a calculator you need implement a label, and make you own keyboard. If you want use the standard keyboard, put your textField out of the window CGRectMake(1000,0,80,30). Place in its position a UILabel (like mirror), and use the delegate methods to write in the label at the same time. – Onik IV Nov 28 '14 at 23:12
  • Last thing, to do the last approach, you need use a UITextView no a textField, because this has delegate methods fire when the textChange. But It's the same a textView out of window or hidden a UILabel as a mirror. – Onik IV Nov 28 '14 at 23:19
  • Thanks a lot for your inputs. I will update the question soon – GJain Nov 28 '14 at 23:21
0

Check this: How to disable paste option in menucontroller in iOS?

Community
  • 1
  • 1
dms90
  • 354
  • 1
  • 9