Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CGRect r = CGRectMake(0, myUIScrollView.contentSize.height - 1, 1, 1);
- [myUIScrollView scrollRectToVisible:r animated:YES];
- CGRect rectBottom = CGRectZero;
- rectBottom.size = myUIScrollView.frame.size;
- rectBottom.origin.y = myUIScrollView.contentSize.height - rectBottom.size.height;
- rectBottom.origin.x = 0;
- [myUIScrollView scrollRectToVisible:rectBottom animated:YES];
- #import "ViewController.h"
- //#define WORKAROUND
- @interface ViewController ()
- @property (nonatomic, strong) UIScrollView *scrollView;
- @property (nonatomic, strong) UITextView *textView;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap)]];
- self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
- self.scrollView.contentSize = CGSizeMake(320, 400);
- self.scrollView.backgroundColor = [UIColor lightGrayColor];
- [self.view addSubview:self.scrollView];
- #ifdef WORKAROUND
- UIScrollView* dummyScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 280, 280, 100)];
- self.textView = [[UITextView alloc] initWithFrame:dummyScrollView.bounds];
- [dummyScrollView addSubview:self.textView];
- [self.scrollView addSubview:dummyScrollView];
- #else
- self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 280, 280, 100)];
- [self.scrollView addSubview:self.textView];
- #endif
- self.textView.backgroundColor = [UIColor grayColor];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
- }
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)viewTap
- {
- if (self.textView.isFirstResponder) {
- [self.textView resignFirstResponder];
- }
- else {
- [self.textView becomeFirstResponder];
- }
- }
- - (void)keyboardWasShown:(NSNotification*)aNotification
- {
- #ifdef WORKAROUND
- [self.scrollView scrollRectToVisible:CGRectInset(self.textView.superview.frame, 0, -10) animated:YES];
- #else
- [self.scrollView scrollRectToVisible:CGRectInset(self.textView.frame, 0, -10) animated:YES];
- #endif
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement