IOS: scrollView does not scroll to the correct position with visible keyboard

I am testing an iOS application using 6.1 simulator. I worked for hours to scroll my scrollView to the right position when the keyboard is visible (after the user clicks on the textView). I tried after the answer marked as correct on this page:

How to scroll UIScrollView when keyboard appears?

This is what I have:

- (void)keyboardWasShown:(NSNotification*)aNotification { NSLog(@"keyboardWasShown"); NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); self.scrollView.contentInset = contentInsets; self.scrollView.scrollIndicatorInsets = contentInsets; // If active text field is hidden by keyboard, scroll it so it visible // Your app might not need or want this behavior. CGRect aRect = self.view.frame; aRect.size.height -= kbSize.height; if (!CGRectContainsPoint(aRect, self.activeView.frame.origin) ) { NSLog(@"scrollToView"); CGPoint scrollPoint = CGPointMake(0.0, self.stepDescriptionField.frame.origin.y-kbSize.height); NSLog(@"scrollPoint: %f", scrollPoint.y); [self.scrollView setContentOffset:scrollPoint animated:YES]; } } 

enter image description here

From the images above it can be seen that if the user clicks on the textView, scrollView does not scroll to the desired position (you should be able to see the text content of the textView).

The strange thing is that I manually tried to change the y scrollPoint offset to different values, but it didn't seem to affect where the window scrolls. What am I doing wrong?

Other things that may be important:

  • I have auto-shutdown disabled (so the user can scroll vertically in this view).
  • textView does not scroll (it changes according to its contents)

Edit

I found that if I add an offset to the contentInsets as follows:

 UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+50.0, 0.0); 

the view moves to the desired position. The only drawback is that there is an additional addition below:

enter image description here

Is there a better way to do this?

0
ios objective-c uiscrollview keyboard
Sep 06 '14 at 20:36
source share
1 answer

I used this with a UITextField, not a UITextView, but I believe that it should work the same way anyway. This allows me to position the text box directly above the keyboard.

keyboardWillShow is a function when NSNotificationCenter receives a UIKeyboardWillShowNotification

 -(void) keyboardWillShow:(NSNotification *)note { // Get the keyboard size CGRect keyboardBounds; [[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds]; // Start animation [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.3f]; // Get Keyboard height and subtract the screen height by the origin of the textbox and height of text box to position textbox right above keyboard self.scrollView.contentOffset = CGPointMake(0,keyboardBounds.size.height-([UIScreen mainScreen].bounds.size.height - commentBox.frame.origin.y - commentBox.frame.size.height)); [UIView commitAnimations]; } 
+1
Sep 07 '14 at 21:34
source share
— -



All Articles