iOS development, use category to implement keyboard pop-up, move view to prevent obscured

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.