title: How to show and hide keyboard properly in iOS?
body: <p>I know that his probably is duplicate but I have specific question according my situation.</p>
  
  <p>I must offset keyboard when visible to show <code>currently edited field</code> (which is sometimes hidden behind keyboard). I used custom code. It was not working correctly in cases when the next text field is selected or when I have <code>scroll view</code> as a container. I change my code as Apple recommends (see <a href="http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html" rel="nofollow">link</a>). </p>
  
  <p>I have <code>UIViewController</code> (<code>VC</code>) with embedded <code>UIScrollView</code>. My <code>UITextFields</code> are inside <code>scroll view</code>. I have <code>header label</code> in <code>VC</code>. </p>
  
  <p>My problems are: 
  1. The suggestion element of the keyboard is hiding edited field. I fix this by adding <code>height</code> of my <code>header label</code>.</p>
  
  <ol start="2">
  <li>When I hide keyboard and I am at the <strong>top</strong> or <strong>bottom</strong> of the <code>scroll view</code> the big <code>empty space</code> is left. Is it possible to calculate <code>offset</code> and not to show that <code>empty space</code>?</li>
  </ol>
  
  <p><strong>EDIT:</strong>
  The code that I am using is bellow:</p>
  
  <pre><code>// 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;
      CGRect bkgndRect = activeField.superview.frame;
      bkgndRect.size.height += kbSize.height;
      [activeField.superview setFrame:bkgndRect];
      [scrollView setContentOffset:CGPointMake(0.0, 
        activeField.frame.origin.y
        -kbSize.height
        +self.lblHeader.frame.size.height) 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>