Posted By

habib123 on 01/31/10


Tagged


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

davidsi
bobbym245
nsbuckner
Tintenklecks
gospaz
cnathan


Keyboard Notification


 / Published in: Objective C
 

  1. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  2.  
  3. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  4.  
  5.  
  6. - (void)keyboardWillShow:(NSNotification *)notification
  7. {
  8.  
  9. UIScrollView *scrollView = (UIScrollView *)self.view;
  10. CGRect bounds = [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
  11. CGPoint center = [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue];
  12.  
  13. // We need to compute the keyboard and table view frames in window-relative coordinates
  14. 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);
  15. CGRect viewFrame = [scrollView.window convertRect:scrollView.frame fromView:scrollView.superview];
  16.  
  17. // And then figure out where they overlap
  18. CGRect intersectionFrame = CGRectIntersection(viewFrame, keyboardFrame);
  19.  
  20. // This assumes that no one else cares about the table view's insets...
  21. UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, intersectionFrame.size.height, 0);
  22. [scrollView setContentInset:insets];
  23. [scrollView setScrollIndicatorInsets:insets];
  24. [scrollView flashScrollIndicators];
  25.  
  26. }
  27.  
  28. - (void)keyboardWillHide:(NSNotification *)notification
  29. {
  30.  
  31. UIScrollView *scrollView = (UIScrollView *)self.view;
  32. // This assumes that no one else cares about the table view's insets...
  33. [scrollView setContentInset:UIEdgeInsetsZero];
  34. [scrollView setScrollIndicatorInsets:UIEdgeInsetsZero];
  35.  
  36. }
  37.  
  38. //For iPad
  39. - (void)keyboardWillMove:(NSNotification *)note {
  40. NSDictionary *info = [note userInfo];
  41. CGRect kbBeginFrame = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
  42. CGRect kbEndFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  43. kbBeginFrame = [self.view convertRect:kbBeginFrame fromView:nil];
  44. kbEndFrame = [self.view convertRect:kbEndFrame fromView:nil];
  45.  
  46. double animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  47. UIViewAnimationCurve animationCurve = (UIViewAnimationCurve)[[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
  48.  
  49. [UIView beginAnimations:@"OAKeyboardAnimation" context:nil];
  50. [UIView setAnimationCurve:animationCurve];
  51. [UIView setAnimationDuration:animationDuration];
  52.  
  53. CGFloat kbYDelta = kbEndFrame.origin.y - kbBeginFrame.origin.y;
  54. CGFloat x = leftButton.center.x;
  55.  
  56. leftButton.center = CGPointMake(x, leftButton.center.y + kbYDelta);
  57. middleButton.center = CGPointMake(x, middleButton.center.y + kbYDelta);
  58. rightButton.center = CGPointMake(x, rightButton.center.y + kbYDelta);
  59.  
  60. [UIView commitAnimations];
  61. }

Report this snippet  

You need to login to post a comment.