Transferred from: http://www.cnblogs.com/xinus/archive/2013/01/22/ios-keybord-notification.html
- Register for notification of keyboard events
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector ( Keyboardwillshow:) name:uikeyboardwillshownotification Object:nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyb Oardshow:) name:uikeyboarddidshownotification Object:nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyb Oardwillhide:) name:uikeyboardwillhidenotification Object:nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyb Oardhide:) Name:uikeyboarddidhidenotification Object:nil];
- In the callback that the keyboard will appear and hide, add the animation.
-(void) Keyboardwillshow: (nsnotification *) Notif {if (Self.hidden = = YES) {return; } cgrect rect = [[Notif.userinfo Objectforkey:uikeyboardframeenduserinfokey] cgrectvalue]; CGFloat y = rect.origin.y; [UIView Beginanimations:nil Context:nil]; [UIView setanimationduration:0.25]; Nsarray *subviews = [self subviews]; For (UIView *sub in subviews) {cgfloat Maxy = Cgrectgetmaxy (sub.frame); if (Maxy > y-2) {sub.center = Cgpointmake (Cgrectgetwidth (self.frame)/2.0, Sub.center.y-maxy + y-2); }} [UIView commitanimations];} -(void) Keyboardshow: (nsnotification *) Notif {if (Self.hidden = = YES) {return; }}-(void) Keyboardwillhide: (nsnotification *) Notif {if (Self.hidden = = YES) {return; } [UIView Beginanimations:nil context:nil]; [UIView setanimationduration:0.25]; Nsarray *subviews = [self subviews]; For (UIView *sub in subviews) {if (Sub.center.y < CGREctgetheight (self.frame)/2.0) {sub.center = Cgpointmake (Cgrectgetwidth (self.frame)/2.0, CGRectGetHeight (self.f Rame)/2.0); }} [UIView commitanimations];} -(void) Keyboardhide: (nsnotification *) Notif {if (Self.hidden = = YES) {return; }}
iOS Global processing keyboard events