Ghobs Ghobs - 2 months ago 8
iOS Question

Keyboard not dismissing when touching outside of UITextField

I know this is a very basic concept, but for some reason I'm having trouble getting the keyboard to dismiss when a user taps outside of the two

UITextField
s in my view controller. I've tried as many solutions on SO that I can find, and none are working, which tells me I'm misunderstanding them in some way. This is how I initialize the
UITextField
s :

// Min Price
//UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(-25, -76, 70, 30)];
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 25, 70, 30)];

tf.userInteractionEnabled = YES;
tf.textColor = [UIColor blackColor];
tf.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
tf.backgroundColor=[UIColor whiteColor];
tf.text= _minPrice;

tf.textAlignment = NSTextAlignmentCenter;
tf.layer.cornerRadius=8.0f;
tf.layer.masksToBounds=YES;
tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf.layer.borderWidth= 1.0f;

// Max Price
UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(125, 25, 70, 30)];
tf1.userInteractionEnabled = YES;
tf1.textColor = [UIColor blackColor];
tf1.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
tf1.backgroundColor=[UIColor whiteColor];
tf1.text= _maxPrice;

tf1.textAlignment = NSTextAlignmentCenter;
tf1.layer.cornerRadius=8.0f;
tf1.layer.masksToBounds=YES;
tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf1.layer.borderWidth= 1.0f;

//and so on adjust your view size according to your needs
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(70, 100, 200, 60)];

[view addSubview:tf];
[view addSubview:tf1];


[self.view bringSubviewToFront: tf];
[self.view bringSubviewToFront: tf1];

[self.view addSubview:view];


and this is the function I'm using to dismiss the keyboard:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView * txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
}

Answer

As others had said, the setup of my subviews turned out to be the problem, changed it to the code below and it now works.

    // Min Price
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 125, 70, 30)];
    [self.view addSubview:tf];

    tf.userInteractionEnabled = YES;
    tf.textColor = [UIColor blackColor];
    tf.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
    tf.backgroundColor=[UIColor whiteColor];
    tf.text= _minPrice;

    tf.textAlignment = NSTextAlignmentCenter;
    tf.layer.cornerRadius=8.0f;
    tf.layer.masksToBounds=YES;
    tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    tf.layer.borderWidth= 1.0f;

    // Max Price
    UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(125, 125, 70, 30)];
    [self.view addSubview:tf1];

    tf1.userInteractionEnabled = YES;
    tf1.textColor = [UIColor blackColor];
    tf1.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
    tf1.backgroundColor=[UIColor whiteColor];
    tf1.text= _maxPrice;

    tf1.textAlignment = NSTextAlignmentCenter;
    tf1.layer.cornerRadius=8.0f;
    tf1.layer.masksToBounds=YES;
    tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    tf1.layer.borderWidth= 1.0f;
Comments