Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. CGRect r = CGRectMake(0, myUIScrollView.contentSize.height - 1, 1, 1);
  2. [myUIScrollView scrollRectToVisible:r animated:YES];
  3.  
  4. CGRect rectBottom = CGRectZero;
  5. rectBottom.size = myUIScrollView.frame.size;
  6. rectBottom.origin.y = myUIScrollView.contentSize.height - rectBottom.size.height;
  7. rectBottom.origin.x = 0;
  8.  
  9. [myUIScrollView scrollRectToVisible:rectBottom animated:YES];
  10.  
  11. #import "ViewController.h"
  12.  
  13. //#define WORKAROUND
  14.  
  15. @interface ViewController ()
  16. @property (nonatomic, strong) UIScrollView *scrollView;
  17. @property (nonatomic, strong) UITextView *textView;
  18. @end
  19.  
  20. @implementation ViewController
  21.  
  22. - (void)viewDidLoad
  23. {
  24. [super viewDidLoad];
  25.  
  26. [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap)]];
  27.  
  28. self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
  29. self.scrollView.contentSize = CGSizeMake(320, 400);
  30. self.scrollView.backgroundColor = [UIColor lightGrayColor];
  31. [self.view addSubview:self.scrollView];
  32.  
  33. #ifdef WORKAROUND
  34. UIScrollView* dummyScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 280, 280, 100)];
  35. self.textView = [[UITextView alloc] initWithFrame:dummyScrollView.bounds];
  36. [dummyScrollView addSubview:self.textView];
  37. [self.scrollView addSubview:dummyScrollView];
  38. #else
  39. self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 280, 280, 100)];
  40. [self.scrollView addSubview:self.textView];
  41. #endif
  42.  
  43. self.textView.backgroundColor = [UIColor grayColor];
  44.  
  45. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
  46. }
  47.  
  48. - (void)dealloc
  49. {
  50. [[NSNotificationCenter defaultCenter] removeObserver:self];
  51. }
  52.  
  53. - (void)viewTap
  54. {
  55. if (self.textView.isFirstResponder) {
  56. [self.textView resignFirstResponder];
  57. }
  58. else {
  59. [self.textView becomeFirstResponder];
  60. }
  61. }
  62.  
  63. - (void)keyboardWasShown:(NSNotification*)aNotification
  64. {
  65. #ifdef WORKAROUND
  66. [self.scrollView scrollRectToVisible:CGRectInset(self.textView.superview.frame, 0, -10) animated:YES];
  67. #else
  68. [self.scrollView scrollRectToVisible:CGRectInset(self.textView.frame, 0, -10) animated:YES];
  69. #endif
  70. }
  71.  
  72. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement