Method 1. Using <UITEXTFEILDDELEGATE>, use the Uitextfield example to set its delegate to self, and click the return button to hide the keyboard. The implementation functions are as follows:-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField
{
[TextField Resignfirstresponder]; return YES;
} Method 2. Click on other blank areas of the interface to hide
Since Uiviewcontroller is inherited from Uiresponder, it is possible to overwrite-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event; This starts the touch method to cancel the first responder, the code is as follows:
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
[_tfpassword Resignfirstresponder];
[_tfusername Resignfirstresponder];
}
Both of these methods are used when learning iOS.3. It is better to use the Notificationcenter method is better, can obtain the keyboard height and other details
#import "ViewController.h" @interface Viewcontroller () @property (weak, nonatomic) Iboutlet Uitextfield *tftest;@ Property (weak, nonatomic) Iboutlet Uitextfield *tftest2; @end @implementation viewcontroller-(void) Viewdidload {[Super Viewdidload]; }-(void) Viewdidappear: (BOOL) animated{[Super viewdiddisappear:animated]; Remove keyboard listener message [[Nsnotificationcenter defaultcenter]removeobserver:self Name:UIKeyboardWillShowNotificationobject:nil ]; [[Nsnotificationcenter defaultcenter]removeobserver:self Name:UIKeyboardWillHideNotificationobject:nil]; Registering a keyboard listener message [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (keyshow:) name:uikeyboardwills Hownotification Object:nil]; [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (keyhide:) Name: Uikeyboardwillhidenotification Object:nil];} Popup Keyboard Message response-(void) Keyshow: (nsnotification *) no{NSLog (@ "Keyshow"); Nsdictionary *dic = [no valueforkey:@ "UserInfo"]; CGFloat heightkeyboard = [[dicvalueforkey:@ "Uikeyboardboundsuserinfokey"]cgrectvalue].size.height; UIView *firstresponderview = [self getfirstresponderview]; CGFloat distancetobottom = Self.view.frame.size.height-cgrectgetmaxy (firstresponderview.frame); Animation if (Distancetobottom < Heightkeyboard) {CGRect frame = self.view.frame; FRAME.ORIGIN.Y =-(Heightkeyboard-distancetobottom); [UIView animatewithduration:0.3f animations:^{[Self.view setframe:frame]; } completion:^ (BOOL finished) {}]; }}//off Keyboard message response-(void) Keyhide: (nsnotification *) no{NSLog (@ "Keyhide"); CGRect frame = self.view.frame; FRAME.ORIGIN.Y = 0; Animation [UIView animatewithduration:0.3f animations:^{[Self.view setframe:frame]; } completion:^ (BOOL finished) {}];} Click the background area to automatically hide the keyboard-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{UIView * Firstresponderview = [self getfirstresponderview]; [Firstresponderview Resignfirstresponder];}//gets the control in which the current focus is located-(UIView *) getfirstresponderview{UIWindow *keywindow = [[UIApplication Sharedapplication]keywindow]; UIView *firstresponder = [Keywindow performselector: @selector (FirstResponder)]; return firstresponder;} -(void) didreceivememorywarning {[Super didreceivememorywarning];} @end
iOS handling of the keyboard