IOS Dev (54) When the keyboard pops up, the view moves with it

Source: Internet
Author: User

IOS Dev (54) When the keyboard pops up, the view moves with it
    • Tai Rui elder brother
    • Blog: http://prevention.iteye.com

-

Add Listener

[[NSNotificationCenter defaultCenter] addObserver:self                                         selector:@selector(changeContentViewPosition:)                                             name:UIKeyboardWillShowNotification                                           object:nil];[[NSNotificationCenter defaultCenter] addObserver:self                                         selector:@selector(changeContentViewPosition:)                                             name:UIKeyboardWillHideNotification                                           object:nil];

Remove Listener

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

Event handler function

- (void) changeContentViewPosition:(NSNotification *)notification{    NSDictionary *userInfo = [notification userInfo];    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];    CGFloat keyBoardEndY = value.CGRectValue.origin.y;    NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];    NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];    [UIView animateWithDuration:duration.doubleValue animations:^{        [UIView setAnimationBeginsFromCurrentState:YES];        [UIView setAnimationCurve:[curve intValue]];        self.view.center = CGPointMake(self.view.center.x, keyBoardEndY - STATUS_BAR_HEIGHT - self.view.bounds.size.height/2.0);    }];}
Reference
    • http://www.cnblogs.com/programmer-blog/p /3265110.html

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.