0

I've add a scrollView in the view controller and register keyboard show and hidden notifications

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBar.translucent = NO;

    [self registerForKeyboardNotifications];

    // add UITapGestureRecognizer
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];
    [scrollView addGestureRecognizer:tap];
    scrollView.delegate = self;

    [self setupAView];
}

- (void)setupAView {
  // setting up aView...

  scrollView.contentSize = CGSizeMake(self.view.frame.size.width, aView.frame.size.height);

-(void)dismissKeyboard {
    [aView.aTextView resignFirstResponder];
}

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, aView.frame.size.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    CGPoint aPoint = CGPointMake(0, aView.frame.size.height);
    [scrollView setContentOffset:aPoint animated:NO];
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    // scrollView insets
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

this works fine, when I tap aTextView and aView in scrollView will move above keyboard view, but meanwhile if I scrolled the scrollView, whole view will automatically drop down a bit, How come the action will change the insets value or the scrollView contentSize value?

scrollview drops down

pqteru
  • 451
  • 1
  • 7
  • 23

0 Answers0