Overload the Viewwillappear method to add a notification listener event keyboardwillshow:keyboardwillhide when the view is about to appear:
-(void) Viewwillappear: (BOOL) animated
{
[Super viewwillappear:animated];
[[nsnotificationcenter defaultcenter] addobserver: Self
selector:@selector (keyboardwillshow:)
Name:uikeyboardwillshownotification
object:nil];
[[nsnotificationcenter defaultcenter] addobserver: Self
selector:@selector (keyboardwillhide:)
Name:uikeyboardwillhidenotification
object:nil];
}
When your page jumps, to remove two notifications because notifications are not removed, they persist in memory
-(void) Viewdiddisappear: (BOOL) paramanimated {
[Super viewdiddisappear:paramanimated];
[[nsnotificationcenter defaultcenter] removeobserver:self];
}
Two ways to implement a listener event
-(void) Keyboardwillshow: (nsnotification *) Note {
Get the height of the keyboard
The userInfo dictionary in the notification message nsnotification contains the location and size information for the keyboard, and the corresponding key is
Uikeyboardframebeginuserinfokey
Uikeyboardframeenduserinfokey
The corresponding value is a Nsvalue object that contains the CGRect structure, which is the position information at the start and end of the keyboard, respectively.
Uikeyboardanimationdurationuserinfokey
The corresponding value is also the NSNumber object, which is a double-type data that represents the duration of the animation when the keyboard h is displayed or disappears.
Uikeyboardanimationcurveuserinfokey
The corresponding value is the NSNumber object, which is an internal uiviewanimationcurve type of data that represents the type of animation that the keyboard displays or disappears.
Uikeyboardframeenduserinfokey the position of the pre-animated keyboard, including the CGRect Nsvalue
Uikeyboardframeenduserinfokey the keyboard position at the end of the animation, including the CGRect nsvalue
// nsdictionary* info = [anotification userInfo];
CGRect _rect =[[info Objectforkey:uikeyboardframeenduserinfokey] cgrectvalue];
Note.info[objectforkey:uikeyboardframeenduserinfokey]
CGRect keyboardrect=[note. UserInfo[Uikeyboardframeenduserinfokey] cgrectvalue];
cgfloat Deltay=keyboardrect. size. height;
[UIView animatewithduration: [Note.userinfo[ Uikeyboardanimationdurationuserinfokey] Floatvalue] animations: ^{
self. View. Transform=cgaffinetransformmaketranslation(0,-deltay);
}];
}
-(void) Keyboardwillhide: (nsnotification *) Note {
[UIView animatewithduration: [Note.userinfo[uikeyboardanimationdurationuserinfokey] Floatvalue] Animations: ^{
Cgaffinetransformidentity Record the previous location
Cgaffinetransformmaketranslation: Each time is based on the center point of the original position for reference
Cgaffinetransformtranslate each time with the incoming transform as a reference (both overlay effect)
Cgaffinetransformidentity The center point of the initial position
Self.view.transform = cgaffinetransformidentity;
}];
}