⬆️ ⬇️

UITextField and hiding the numeric keypad

I want to talk about how to hide the numeric keypad on the iPhone / iPad, which not so long ago had to use.



As you know, when a UITextField receives focus, it shows the keyboard: alphanumeric or numeric, depending on the type. You can hide this keyboard either by clicking on the Done button on it (which can be of various types and inscriptions, configured), or by calling the method: [textField resignFirstResponder] Unfortunately for developers, there is no Done button on the numeric keypad, its place is generally empty , and therefore, you can remove the keyboard only by calling this method. Before the release of versions iOS 3.2 and 4.0, this was solved by adding a button to this place (the method was rather dirty, but it worked and everyone was happy): neoos .

But after the release of these systems, the method stopped working. There is another hack for this method, but in my humble opinion it is not worth it.



Therefore, a break a bunch of links decided to do this: let the user click somewhere other than the keyboard, and it should disappear, in my opinion quite intuitively. Since there are a lot of controls on the place where there is no keyboard, then subscribing to their events is not an option for one of them, so I decided to just show the transparent button on top of it and hide the keyboard on its UIControlEventTouchDown event. Needless to show the button is only when the keyboard appears, and when you click on the button to hide the keyboard and destroy the button. Implemented it as follows:



Header file (this is so that you can remember it and destroy it later):

UIButton* btnInvisible;



Also here it is necessary to sign the class on the UITextFieldDelegate protocol so that you can capture the moment the keyboard is displayed. The standard subscription to UITextField events for some reason does not work. Well, in Interface Builder you need to specify the UITextField, that its delegate is a controller class, or which one you will be responsible for handling events.

')

Implementation file:

#pragma mark -

#pragma mark UITextField delegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

//Create button

btnInvisible = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];

[btnInvisible addTarget:self action:@selector(hideKeyboard) forControlEvents:UIControlEventTouchDown];



//Show over the window view, which is at index 0 (usually)

CorreasAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

[appDelegate.window insertSubview:btnInvisible atIndex:1];



return YES;

}



- (void)hideKeyboard

{

//Hide keyboard

[quantityField resignFirstResponder];



//Hide and release button

[btnInvisible removeFromSuperview];

[btnInvisible release];

}



That's all. The solution may not be the best, because the controls that are under the invisible button are no longer available, and you have to press it once to remove the keyboard, and then you can click on the control. But it is easy to implement, and in my opinion quite user-friendly.

Source: https://habr.com/ru/post/100498/



All Articles