Well, directly on the code!!!!
This is a. h file
#import<UIKit/UIKit.h>@interfaceUIView (Aboutkeyboard) @property (nonatomic) cgfloat movedistince; @property (nonatomic) UIView*Moveview;/** Specify a view to move when the keyboard appears and disappear, move Superview if there is superview, or move itself*/- (void) Registerwhenkeyboardshowandhidden;/** Specify a view to move when the keyboard appears and disappear, and specify the view to move*/- (void) Registtomovewhenkeyboardshowandhidewithview: (UIView *) view;/** Cancel Move*/- (void) Removeregisterwhenkeyboardshowandhidden;@end
This is a. m file
#import "uiview+aboutkeyboard.h"#import<objc/runtime.h>Static Const void*movedistince = &movedistince;Static Const void*moveview = &Moveview;@implementationUIView (Aboutkeyboard) @dynamic movedistince; @dynamic Moveview;- (void) Setmovedistince: (cgfloat) movedistince{objc_setassociatedobject (self, movedistince, @ (movedistince), OBJC_ association_retain_nonatomic);}-(cgfloat) movedistince{return[Objc_getassociatedobject (Self, movedistince) floatvalue];}- (void) Setmoveview: (UIView *) moveview{objc_setassociatedobject (self, Moveview, Moveview, objc_association_retain_nonatomic);}-(UIView *) moveview{returnObjc_getassociatedobject (self, Moveview);}- (void) registerwhenkeyboardshowandhidden{[[Nsnotificationcenter Defaultcenter] Addobserver:self Selector: @selector (keyboardwillshow:) NAME:UIKEYBOARDW IllshownotificationObject: nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyb Oardwillhidden:) name:uikeyboardwillhidenotification Object: nil];}- (void) Registtomovewhenkeyboardshowandhidewithview: (UIView *) view{Self.moveview=view; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyb Oardwillshow:) name:uikeyboardwillshownotification Object: nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyb Oardwillhidden:) name:uikeyboardwillhidenotification Object: nil];}- (void) Keyboardwillshow: (Nsnotification *) notification{if(self.isfirstresponder) {if(!Self.moveview) {if(Self.superview) Self.moveview= Self.superview;//have Superview ElseSelf.moveview= self;//move itself without a superview} nsdictionary*userinfo =Notification.userinfo; CGRect rect=[Userinfo[uikeyboardframeenduserinfokey] cgrectvalue]; CGFloat Duration=[Userinfo[uikeyboardanimationdurationuserinfokey] floatvalue]; CGRect Willmoverect=Self.moveView.frame; CGFloat Selfy=Cgrectgetmaxy (self.frame); CGFloat lasty= Cgrectgetheight (Willmoverect)-cgrectgetheight (rect); if(Duration >0.0f) { if(Selfy >lasty) {self.movedistince= Selfy-lasty;//the distance to moveWILLMOVERECT.ORIGIN.Y-=self.movedistince; } } Else{cgfloat oldfloat=[Userinfo[uikeyboardframebeginuserinfokey] cgrectvalue].size.height; CGFloat Move= (Rect.size.height-oldfloat); WILLMOVERECT.ORIGIN.Y-=move; Self.movedistince+=move; } [UIView animatewithduration:duration animations:^{self.superview.frame=Willmoverect; }]; }}- (void) Keyboardwillhidden: (Nsnotification *) notification{if(self.isfirstresponder) {nsdictionary*userinfo =Notification.userinfo; CGRect Willmoverect=Self.moveView.frame; WILLMOVERECT.ORIGIN.Y+=self.movedistince; CGFloat Duration=[Userinfo[uikeyboardanimationdurationuserinfokey] floatvalue]; [UIView animatewithduration:duration animations:^{self.superview.frame=Willmoverect; }]; }}- (void) removeregisterwhenkeyboardshowandhidden{[[Nsnotificationcenter Defaultcenter] removeobserver:self name: UikeyboardwillshownotificationObject: nil]; [[Nsnotificationcenter Defaultcenter] removeobserver:self name:uikeyboardwillhidenotificationObject: nil];}@end
Using the method, take Uitextfield as an example, if there is a Uitextfield *textfield, use the following method:
[Textfiled Registerwhenkeyboardshowandhidden];
Or
[Textfiled RegistToMoveWhenKeyboardShowAndHideWithView:textField.superView];
Finally, when you leave, remember.
[TextField Removeregisterwhenkeyboardshowandhidden];
Otherwise, there will be an error.