IOS method to close the keyboard

  • 2020-05-07 20:30:04
  • OfStack

First, press done on the keyboard to close the keyboard

First select TextFields from Interface Builder, then find Text Input Traits from Text Field Attributes, and select Return Key as done. OK

Define methods


- (IBAction) textFieldDoneEditing:(id)sender;    // Press the Done Key off keyboard

Implementation method


// According to the Done Close the keyboard after pressing
- (IBAction) textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
}

Then find the event Did End On Exit, associated with textFieldDoneEditing, OK.
What if it's a numeric keypad and there's no done key, and we close the keypad by touching the background

Define methods


- (IBAction) backgroundTap:(id)sender;   // Close the keyboard by touching the background

Implementation method


// Close the keyboard by touching the background
- (IBAction) backgroundTap:(id)sender
{
[nameFiled resignFirstResponder];
[numberField resignFirstResponder];
}

Then select the Touch Down event in the background and associate backgroundTap, OK
One thing to note about this method is that you have to change UIView to UIControl.


Related articles: