-1

Added a subview on App delegate window, textfield is in subview, startDateTxtFld will open date picker but date picker remains behind the keyboard. keyboard does not hide on resignFirstResponder or self.view eneEditing setting as true.

-(void)textFieldDidBeginEditing:(UITextField *)textField
{

    if (textField == startDateTxtFld )
    {
        startDateSelected =YES;
        endDateSelected = NO;
//        [referenceNmbrTxtFld resignFirstResponder];
     //   [startDateTxtFld becomeFirstResponder];
        //textField.inputView=datePicker;
       // [self.view endEditing:YES];
       // [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
//       [APP_DELEGATE.window resignFirstResponder];
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
        [UIView animateWithDuration:0.5 animations:^
        {
            [startDateTxtFld resignFirstResponder];
            _bottomGap4DatePicker.constant=0;
            datePicker.backgroundColor=[UIColor whiteColor];
           // [_subVw4DtPkr becomeFirstResponder];
        }];
       [self.navigationController.view endEditing:YES];
    }
    else if (textField == endDateTxtFld)
    {
        startDateSelected =NO;
        endDateSelected = YES;
       // [self.view endEditing:YES];
//        [referenceNmbrTxtFld resignFirstResponder];
       // [endDateTxtFld becomeFirstResponder];
      //  textField.inputView=datePicker;
      //  [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
//        [APP_DELEGATE.window resignFirstResponder];
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
        [UIView animateWithDuration:0.5 animations:^
         {
            // [endDateTxtFld resignFirstResponder];
             _bottomGap4DatePicker.constant=0;
             datePicker.backgroundColor=[UIColor whiteColor];
             //[_subVw4DtPkr becomeFirstResponder];
         }];
        [self.navigationController.view endEditing:YES];
    }
   else
    {
        [textField becomeFirstResponder];
        _bottomGap4DatePicker.constant=[UIScreen mainScreen].bounds.size.height +_subVw4DtPkr.frame.size.height;
        _popUpVwConstraint.constant=-30;
    }

}
8
  • [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; why using this instead of [self.view endEditing:YES]; ? Jun 1, 2016 at 12:43
  • I have added subview on App Delegate Window,I have used [self.view endEditing:YES]; it didn't work that's why I use [[[UIApplication sharedApplication] keyWindow] endEditing:YES];but it is also not working Jun 1, 2016 at 12:57
  • Means i am asking that why you have add subview to window? You can add subview to self.view also Jun 1, 2016 at 12:59
  • to hide navigation bar,Navigation bar is in App delegate class,so i have to add it on window otherwise navigation bar will be displaying on subview also Jun 1, 2016 at 13:15
  • You can directly hide navigation bar. for that you not need to add view on window. you can hide like self.navigationController.navigationBarHidden = YES; Jun 1, 2016 at 13:24

3 Answers 3

0

Hide keyboard Anywhere in IOS :

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

OR

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     [self.view endEditing:YES];    
 }
1
  • I have 3 text fields and have to hide keyboard on only1 Jun 1, 2016 at 12:31
0

I tried all the solution, like adding

  1. UITextFieldDelegate

  2. tf_edit.returnKeyType = UIReturnKeyDone

  3. - (BOOL)disablesAutomaticKeyboardDismissal
    {
         return NO;
    }
    

Nothing helped.

textFieldShouldReturn was being called. but keyboard was not appearing. I read that it is going to some other textfield when I am clicking DONE button. But there was no other textfield. So instead of applying resignFirstResponder to my textfield name, I applied to the textfield which is a parameter of textFieldShouldReturn function and it worked. I am not sure if it is correct way. It worked for me.

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //[tf_edit resignFirstResponder];
    [textField resignFirstResponder];
    return YES;
}
0
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { {
    NSLog(@"textFieldDidBeginEditing===%@" , textField.text);
    CGAffineTransform translation = CGAffineTransformIdentity;

    if (textField==vacDate || textField==vacTime) {
        [self.view endEditing:YES];

        [UIView beginAnimations:nil context:nil];
        self.view.transform = translation;
        [UIView commitAnimations];
        myDatePicker.hidden = NO;
        return NO;
    }
    else
    { 
        if (textField==vacType) {
           translation = CGAffineTransformMakeTranslation(0, -60);
        }
        [UIView beginAnimations:nil context:nil];
        self.view.transform = translation;
        [UIView commitAnimations];
        myDatePicker.hidden = YES;
        return YES;
    }
}
2
  • still not working my view is subview in which 3 text fields are present,I have added subview on App delegate window Jun 1, 2016 at 12:33
  • thanks @Kuldeepsingh (BOOL)textFieldShouldBeginEditing:(UITextField *)textField, worked i had resigned the first responder from third text field in condition if(textField == startDateTxtFld || textField == endDateTxtFld){ [rfrncNmbrTxtFld resignFirstResponder]; //and animated picker } Jun 1, 2016 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.