/ Published in: Objective C
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; { UIScrollView *scrollView = (UIScrollView *)self.view; CGRect bounds = [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue]; CGPoint center = [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue]; // We need to compute the keyboard and table view frames in window-relative coordinates CGRect keyboardFrame = CGRectMake(round(center.x - bounds.size.width/2.0), round(center.y - bounds.size.height/2.0), bounds.size.width, bounds.size.height); CGRect viewFrame = [scrollView.window convertRect:scrollView.frame fromView:scrollView.superview]; // And then figure out where they overlap CGRect intersectionFrame = CGRectIntersection(viewFrame, keyboardFrame); // This assumes that no one else cares about the table view's insets... UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, intersectionFrame.size.height, 0); [scrollView setContentInset:insets]; [scrollView setScrollIndicatorInsets:insets]; [scrollView flashScrollIndicators]; } { UIScrollView *scrollView = (UIScrollView *)self.view; // This assumes that no one else cares about the table view's insets... [scrollView setContentInset:UIEdgeInsetsZero]; [scrollView setScrollIndicatorInsets:UIEdgeInsetsZero]; } //For iPad CGRect kbBeginFrame = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; CGRect kbEndFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; kbBeginFrame = [self.view convertRect:kbBeginFrame fromView:nil]; kbEndFrame = [self.view convertRect:kbEndFrame fromView:nil]; double animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve animationCurve = (UIViewAnimationCurve)[[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; [UIView beginAnimations:@"OAKeyboardAnimation" context:nil]; [UIView setAnimationCurve:animationCurve]; [UIView setAnimationDuration:animationDuration]; CGFloat kbYDelta = kbEndFrame.origin.y - kbBeginFrame.origin.y; CGFloat x = leftButton.center.x; leftButton.center = CGPointMake(x, leftButton.center.y + kbYDelta); middleButton.center = CGPointMake(x, middleButton.center.y + kbYDelta); rightButton.center = CGPointMake(x, rightButton.center.y + kbYDelta); [UIView commitAnimations]; }