title: Scrolling UITextField into view when keyboard is shown
body: <p>I'm writing an app targeting iOS 6.1. I'm trying to follow this <a href="http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7" rel="nofollow noreferrer">Apple doc</a> on how to scroll a UITextField into view if it is obscured by the keyboard. The problem is that Apple's documented algorithm for calculating the scroll point doesn't work so well. My algorithm for calculating the scroll point works only slightly better, but is off by 70 pixels. What is the correct way to calculate the scroll point?</p>
  
  <p>Below you can see, from left to right, my view before the keyboard is shown, my view after scrolling using Apple's algorithm to calculate the scroll point, and my view after scrolling using my algorithm to calculate the scroll point. (Each square in that grid is 25 pixels by 25 pixels.)</p>
  
  <p><img src="https://i.stack.imgur.com/iITlP.png" alt="enter image description here"></p>
  
  <p>And here is the code I am using. Note the <code>#if APPLE_WAY</code> block.</p>
  
  <pre><code>- (void)keyboardWasShown:(NSNotification*)aNotification
  {
      NSDictionary* info = [aNotification userInfo];
      CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
  
      UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
      self.view.contentInset = contentInsets;
      self.view.scrollIndicatorInsets = contentInsets;
  
      // If active text field is hidden by keyboard, scroll it so it's visible
      // Your application might not need or want this behavior.
      CGRect aRect = self.view.frame;
      aRect.size.height -= kbSize.height;
      if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
  
  #if APPLE_WAY
          CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y-kbSize.height);
  #else
          CGFloat offset = self.activeField.frame.origin.y;
  
          //TODO: Why is this off by 70?
          offset = offset - 70;
  
          CGPoint scrollPoint = CGPointMake(0.0, offset);
  #endif
          [self.view setContentOffset:scrollPoint animated:YES];
      }
  }
  </code></pre>