Posted By

devb0yax on 06/15/10


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

shimdh
bobbym245


Textview input is behind the keyboard


 / Published in: iPhone
 

  1. .h file:
  2. @interface MyTextViewViewController : UIViewController <UITextViewDelegate>
  3. {
  4. UITextView *m_textView;
  5. UIBarButtonItem *doneBarButtonItem;
  6. }
  7.  
  8. @property(nonatomic,retain) IBOutlet UITextView *m_textView;
  9. @property(nonatomic,retain) IBOutlet UIBarButtonItem *doneBarButtonItem;
  10.  
  11. - (IBAction)saveAction:(id)sender;
  12.  
  13. ----------------------------
  14. .m file:
  15. ...
  16. - (void)viewWillAppear:(BOOL)animated
  17. {
  18. // listen for keyboard hide/show notifications so we can properly adjust the table's height
  19. [super viewWillAppear:animated];
  20. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  21. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  22. }
  23.  
  24. - (void)viewDidDisappear:(BOOL)animated
  25. {
  26. [super viewDidDisappear:animated];
  27. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  28. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
  29. }
  30.  
  31. - (void)keyboardWillShow:(NSNotification *)aNotification
  32. {
  33. // the keyboard is showing so resize the table's height
  34. CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
  35. NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  36. CGRect frame = self.view.frame;
  37. frame.size.height -= keyboardRect.size.height;
  38. [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
  39. [UIView setAnimationDuration:animationDuration];
  40. self.view.frame = frame;
  41. [UIView commitAnimations];
  42. }
  43.  
  44. - (void)keyboardWillHide:(NSNotification *)aNotification
  45. {
  46. // the keyboard is hiding reset the table's height
  47. CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
  48. NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  49. CGRect frame = self.view.frame;
  50. frame.size.height += keyboardRect.size.height;
  51. [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
  52. [UIView setAnimationDuration:animationDuration];
  53. self.view.frame = frame;
  54. [UIView commitAnimations];
  55. }
  56.  
  57.  
  58. #pragma mark -
  59. #pragma mark UITextViewDelegate
  60. - (void)textViewDidBeginEditing:(UITextView *)textView
  61. {
  62. doneBarButtonItem.enabled = YES;
  63. }
  64.  
  65. - (IBAction)saveAction:(id)sender
  66. {
  67. doneBarButtonItem.enabled = NO;
  68. [self.m_textView resignFirstResponder];
  69. }

Report this snippet  

You need to login to post a comment.