ios-The entire view moves when the input box is obscured by the keyboard

Source: Internet
Author: User

      • Registering keyboard notifications
    #pragma mark - 键盘通知- (void)addNoticeForKeyboard {    //注册键盘出现的通知    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardWillShow:)                                                 name:UIKeyboardWillShowNotification object:nil];    //注册键盘消失的通知    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardWillHide:)                                                 name:UIKeyboardWillHideNotification object:nil];}
      • Keyboard Agent Events
    Keyboard Display event-(void) Keyboardwillshow: (nsnotification *) Notification {//Get keyboard height, on different devices, and in Chinese and English are different cgfloat kbheight = [[    Notification.userinfo Objectforkey:uikeyboardframeenduserinfokey] cgrectvalue].size.height; Calculates the distance from the top of the keyboard to the bottom of the Inputtextview panel (plus the custom buffer distance interval_keyboard) cgfloat offset = (textview.frame.origin.y+    Textview.frame.size.height+interval_keyboard)-(self.view.frame.size.height-kbheight); Gets the animation time of the keyboard so that it can be more coherent when the view is moved double duration = [[Notification.userinfo objectforkey:    Uikeyboardanimationdurationuserinfokey] Doublevalue]; Move the view up to the calculated offset if (Offset > 0) {[UIView animatewithduration:duration animations:^{Self.view.frame        = CGRectMake (0.0f,-offset, Self.view.frame.size.width, self.view.frame.size.height);    }]; }}///Keyboard Vanishing Event-(void) Keyboardwillhide: (Nsnotification *) Notify {//Keyboard animation time double duration = [[Notify.userinfo object]    Forkey:uikeyboardanimationdurationuserinfokey] Doublevalue]; The view sinks back to the original [UIView animateWithduration:duration animations:^{self.view.frame = CGRectMake (0, 0, Self.view.frame.size.width, Self.view.frame.    Size.Height); }];}


    The struggling snail
    Links: http://www.jianshu.com/p/4e235e952b0c
    Source: Pinterest

ios-The entire view moves when the input box is obscured by the keyboard

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.