I want to hide keyboard on UITextField end editing event but somehow I am not able to get following code working! When I press Done button, it hides the keyboard but not when I don’t press done button and move to another UITextField where I don’t need keyboard but UIPickerView. Basically UIPickerView is appearing but behind the keyboard. I am resigning current UITextField on end editing event as well as on begin editing for required text fields. The begin editing code works fine if I don’t have keyboard already shown for previous UITextField. Could someone please tell me what am I doing wrong?
Following sequence works:
- Select normal
UITextFieldand insert text, press done button (this hides keyboard) - Select picker
UITextField(this displays picker view)
..but following doesn’t:
- Select normal
UITextFieldand insert text -
Select picker
UITextField(the picker view is behind the keyboard as I didn’t press done button for previousUITextField). Here it calls end editing but it doesn’t hide keyboard!- (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; scrollView.contentSize = CGSizeMake(320, 750); [scrollView setFrame:CGRectMake(0, 0, 320, 480)]; return YES; } -(void)textFieldDidEndEditing:(UITextField *)textField { [textField resignFirstResponder]; } - (void)textFieldDidBeginEditing:(UITextField *)textField { DatePicker.hidden = YES; CountryPickerView.hidden = YES; switch (textField.tag) { case 3: [textField resignFirstResponder]; DatePicker.hidden = NO; return; case 6: [textField resignFirstResponder]; CountryPickerView.hidden = NO; return; default: break; } scrollView.contentSize = CGSizeMake(320, 650); [scrollView setFrame:CGRectMake(0, 0, 320, 260)]; }
The right way to handle this is to set the
inputViewproperty for the field that uses a picker instead of the keyboard. Configure the picker as you need it (set up delegate, data source, etc.) and then set it as the field’sinputView. The system will handle hiding the keyboard and showing the picker view, or vice versa, as you move from one field to the next.