Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think that the main issue is that you need to use <code>UIKeyboardFrameEndUserInfoKey</code> instead of <code>UIKeyboardFrameBeginUserInfoKey</code>.</p> <p>That being said, I have written a different version of this method that will work even if the scrollview is not placed at the bottom of the screen and may work better for you:</p> <p>Note that I use <code>self.activeTextField</code> instead of <code>self.textField</code>.</p> <pre><code>// Called when the UIKeyboardDidShowNotification is sent. - (void)keyboardWasShown:(NSNotification*)aNotification { // Calculate the frame of the scrollview, in self.view's coordinate system UIScrollView *scrollView = self.scrollView; CGRect scrollViewRect = [self.view convertRect:scrollView.frame fromView:scrollView.superview]; // Calculate the frame of the keyboard, in self.view's coordinate system NSDictionary* info = [aNotification userInfo]; CGRect kbRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; kbRect = [self.view convertRect:kbRect fromView:nil]; // Figure out where the two frames overlap, and set the content offset of the scrollview appropriately CGRect hiddenScrollViewRect = CGRectIntersection(scrollViewRect, kbRect); if (!CGRectIsNull(hiddenScrollViewRect)) { UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, hiddenScrollViewRect.size.height, 0.0); scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; } [self scrollToActiveTextField]; } // Called when the UIKeyboardWillHideNotification is sent - (void)keyboardWillBeHidden:(NSNotification*)aNotification { self.scrollView.contentInset = UIEdgeInsetsZero; self.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero; } - (void)scrollToActiveTextField { if (self.activeTextField) { CGRect visibleRect = self.activeTextField.frame; visibleRect = [self.scrollView convertRect:visibleRect fromView:self.activeTextField.superview]; visibleRect = CGRectInset(visibleRect, 0.0f, -5.0f); [self.scrollView scrollRectToVisible:visibleRect animated:YES]; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload