iOS development TextField based on keyboard adaptive location

Source: Internet
Author: User

-(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

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.