First, keyboard notification
We often need to do certain things when the keyboard pops up or hidden, so we need to listen to the state of the keyboard
When the keyboard state changes, some specific notifications will be sent to the system.
Uikeyboardwillshownotification//keyboard is about to be displayed
Uikeyboarddidshownotification//Keyboard display complete
Uikeyboardwillhidenotification//keyboard is about to be hidden
Uikeyboarddidhidenotification//Keyboard hidden complete
Uikeyboardwillchangeframenotification//Keyboard position size is about to change
Uikeyboarddidchangeframenotification//Keyboard Position size change complete
When the system issues a keyboard notification, it comes with additional keyboard-related information (a dictionary), and the dictionary common key is as follows:
Uikeyboardframebeginuserinfokey//keyboard just started frame
Uikeyboardframeenduserinfokey//keyboard final frame (after the animation has finished executing)
Uikeyboardanimationdurationuserinfokey//Keyboard animation time
Uikeyboardanimationcurveuserinfokey//Keyboard animation execution Rhythm (speed)
When you click the Text field pop-up text, let the text field always follow the keyboard and stick to the keyboard
Just because the keyboard state changes, the system will send some specific notifications, we can monitor the state of the keyboard
-(void) viewdidload { [super Viewdidload]; Object : nil];} -(void) dealloc{ [[Nsnotificationcenter Defaultcenter] removeobserver:self];}
Implementation method:
/** Called when the frame of the listening keyboard is about to change*/- (void) Keyboardwillchange: (Nsnotification *) note{//get the last frame of the keyboardCGRect frame =[Note.userinfo[uikeyboardframeenduserinfokey] cgrectvalue]; //Modify the bottom constraintSelf.bottomSpace.constant = Self.view.frame.size.height-FRAME.ORIGIN.Y; //Performing animationsCGFloat Duration =[Note.userinfo[uikeyboardanimationdurationuserinfokey] doublevalue]; [UIView animatewithduration:duration animations:^{[Self.view layoutifneeded]; }];} //Touch the screen to exit the keyboard- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{ //text box is no longer the first responder, it exits the keyboard//[Self.textfield Resignfirstresponder]; //[Self.textfield Endediting:yes];[Self.view Endediting:yes];}
UserInfo is a system-provided dictionary property
@property (Nullable, ReadOnly, copy) Nsdictionary *userinfo;
The results are printed as follows:
Uikeyboardanimationcurveuserinfokey = 7; Animation Curve Animation
Uikeyboardanimationdurationuserinfokey = "0.25"; Animation time
Uikeyboardboundsuserinfokey = "Nsrect: {{0, 0}, {320, 253}}"; Keyboard bounds
Uikeyboardcenterbeginuserinfokey = "Nspoint: {160, 694.5}"; Start Center position of keyboard
Uikeyboardcenterenduserinfokey = "Nspoint: {160, 441.5}"; End Center position of keyboard
Uikeyboardframebeginuserinfokey = "Nsrect: {{0, 568}, {320, 253}}";//keyboard Start Popup frame
Uikeyboardframeenduserinfokey = "Nsrect: {{0, 315}, {320, 253}}";//frame to exit the keyboard
Uikeyboardislocaluserinfokey = 1;
iOS Development--ui Advanced (vi) keyboard processing