-(void) setnotification {[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector ( Keyboardwillchangeframe:) name:uikeyboardwillchangeframenotification Object:nil];} -(void) closenotification{[[Nsnotificationcenter Defaultcenter] removeobserver:self name: Uikeyboardwillchangeframenotification Object:nil];} -(void) Keyboardwillchangeframe: (nsnotification *) Notification {nsdictionary *dict = [notification UserInfo]; Keyboard pop-up and retract time cgfloat duration = [[Dict objectforkey:uikeyboardanimationdurationuserinfokey] floatvalue]; Keyboard initial time frame cgrect beginkeyboardrect = [[Dict objectforkey:uikeyboardframebeginuserinfokey] cgrectvalue]; After the keyboard stops frame cgrect endkeyboardrect = [[Dict objectforkey:uikeyboardframeenduserinfokey] cgrectvalue]; Subtract to keyboard height cgfloat yoffset = endkeyboardrect.origin.y-beginkeyboardrect.origin.y; Create an Appdelegate singleton object appdelegate *appdelegate = (appdelegate *) [uiapplication sharedapplication].delegate; EarlyInitializes an array, UIWindow all the child views nsarray *array = appDelegate.window.subviews; Gets the view view of the current controller UIView *view = appdelegate.window.subviews[array.count-1]; TextField frame CGRect Selfframefromuiwindow = [self convertRect:self.bounds toView:appDelegate.window] relative to UIWindow ; TextField bottom distance from bottom of screen cgfloat bottomheight = [UIScreen mainscreen].bounds.size.height-selfframefromuiwindow.origin . y-selfframefromuiwindow.size.height; Initializes a frame with the size UIWindow frame cgrect windowframe = appDelegate.window.frame; Increase or decrease the Y value of this frame by the corresponding height (40 is the distance between the bottom of the TextField and the top of the keyboard) WINDOWFRAME.ORIGIN.Y + = Yoffset + bottomHeight-40; According to Yoffset determine if the keyboard is ejected or retracted if (Yoffset < 0) {//keyboard popup, change the current controller's view frame [UIView Animatewithdurati On:duration animations:^{view.frame = windowframe; }]; } else {//keyboard retract, restore view frame to original [UIView animatewithduration:duration animations:^{view.frame = Appdelegate.window.fRame }]; }}
iOS development TextField based on keyboard adaptive location