title: Keyboard Scroll on Active Text Field - Scrolling to Out of View?
body: <p>I have an application with a view that has text fields from the top of the view to the bottom of the view. I needed it to scroll when editing the bottom fields so that the fields would be visible, but it's not seeming to work correctly.</p>
  
  <p>Following the <a href="http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html" rel="noreferrer">Apple docs</a>, I placed all of that code into my program (Listings 4-1, 4-2), and added the <code>scrollView</code> and <code>activeField</code> outlets to my header file and linked them to IB. </p>
  
  <p>The problem is as soon as I click into a text field, ALL of the text fields go out of view until I dismiss the keyboard. They scroll down very far (again, far enough to where none of the fields are visible). </p>
  
  <p>Does anyone know what that problem could be caused by?</p>
  
  <p>I'm placing the code in here from the Apple Docs so you can see exactly what code I'm using without having to click away. </p>
  
  <pre><code>//my .h
      IBOutlet UIScrollView *scrollView;
      IBOutlet UITextField *activeField;
  
  //.m
      // Call this method somewhere in your view controller setup code.
      - (void)registerForKeyboardNotifications
      {
          [[NSNotificationCenter defaultCenter] addObserver:self
                  selector:@selector(keyboardWasShown:)
                  name:UIKeyboardDidShowNotification object:nil];
  
     [[NSNotificationCenter defaultCenter] addObserver:self
               selector:@selector(keyboardWillBeHidden:)
               name:UIKeyboardWillHideNotification object:nil];
  
  }
  
  // Called when the UIKeyboardDidShowNotification is sent.
  - (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);
      scrollView.contentInset = contentInsets;
      scrollView.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, activeField.frame.origin) ) {
          CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
          [scrollView setContentOffset:scrollPoint animated:YES];
      }
  }
  
  // Called when the UIKeyboardWillHideNotification is sent
  - (void)keyboardWillBeHidden:(NSNotification*)aNotification
  {
      UIEdgeInsets contentInsets = UIEdgeInsetsZero;
      scrollView.contentInset = contentInsets;
      scrollView.scrollIndicatorInsets = contentInsets;
  }
  - (void)textFieldDidBeginEditing:(UITextField *)textField
  {
      activeField = textField;
  }
  
  - (void)textFieldDidEndEditing:(UITextField *)textField
  {
      activeField = nil;
  }
  </code></pre>