title: Objective-C: UIScroller and UItextfields hidden by keyboard appearing in UIViewController
body: <p>I need help trying to configure in a UIViewController, the keyboard : (a) not hovering over both UITextfields, so the scroller should be positioned correctly; and (b) When the user touches the background the keyboard disappears.</p>
  
  <p>I haven't tried (b) yet but I'm trying (a) and the code I got from googling around didn't give me the desired effects. In-fact, it makes my textboxes disappear when I touch one of them. Im sure my implementation of activeField is also wrong. I got this example from Apple Development's Keyboard section.</p>
  
  <h2>The code I have so far is below:</h2>
  
  <pre><code>@interface FirstViewController : UIViewController {
      IBOutlet UITextField *emailAddress;
      IBOutlet UITextField *password;
      IBOutlet UIButton *loginButton;
      IBOutlet UIScrollView *scroller;
      BOOL keyboardShown;
      UITextField *activeField;
  
      ASIHTTPRequest *requestRequiringAuthentication;
      ASINetworkQueue *networkQueue;
  }
  
  - (IBAction) LoginUser:(id)sender;
  
  @property (nonatomic,retain) IBOutlet UITextField *emailAddress;
  @property (nonatomic,retain) IBOutlet UITextField *password;
  @property (nonatomic, retain) IBOutlet UIScrollView *scroller;
  @property (nonatomic, retain) UITextField *activeField;
  @property (retain) ASINetworkQueue *networkQueue;
  @property (retain) ASIHTTPRequest *requestRequiringAuthentication;
  
  @end
  
  
  @implementation FirstViewController
  
  @synthesize requestRequiringAuthentication;
  @synthesize networkQueue;
  @synthesize password;
  @synthesize emailAddress;
  @synthesize scroller;
  @synthesize activeField;
  
  #pragma mark -
  #pragma mark LifeCycle
  
  - (void)awakeFromNib
  {
      NSLog(@"awaking from nib");
      [self setNetworkQueue:[[[ASINetworkQueue alloc] init] autorelease]];    
  }    
  
  - (void)viewDidLoad {
      NSLog(@"viewdidload");
      [super viewDidLoad];
  }
  
  - (void)viewDidUnload {
      // Release any retained subviews of the main view.
      // e.g. self.myOutlet = nil;
  }
  
  - (void)viewWillAppear:(BOOL)animated {
      keyboardShown = NO;
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
  [super viewWillAppear:animated];
  }
  
  - (void)viewWillDisappear:(BOOL)animated {
      [[NSNotificationCenter defaultCenter] removeObserver:self];
      [super viewWillDisappear:animated];
  }
  
  
  // Called when the UIKeyboardDidShowNotification is sent.
  - (void)keyboardWasShown:(NSNotification*)aNotification
  {
      if (keyboardShown)
          return;
  
      NSDictionary* info = [aNotification userInfo];
  
      // Get the size of the keyboard.
      NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
      CGSize keyboardSize = [aValue CGRectValue].size;
  
      // Resize the scroll view (which is the root view of the window)
      CGRect viewFrame = [scroller frame];
      viewFrame.size.height -= keyboardSize.height;
      scroller.frame = viewFrame;
  
      // Scroll the active text field into view.
      CGRect textFieldRect = [activeField frame];
      [scroller scrollRectToVisible:textFieldRect animated:YES];
  
      keyboardShown = YES;
  }
  
  // Called when the UIKeyboardDidHideNotification is sent
  - (void)keyboardWasHidden:(NSNotification*)aNotification
  {
      NSDictionary* info = [aNotification userInfo];
  
      // Get the size of the keyboard.
      NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
      CGSize keyboardSize = [aValue CGRectValue].size;
  
      // Reset the height of the scroll view to its original value
      CGRect viewFrame = [scroller frame];
      viewFrame.size.height += keyboardSize.height;
      scroller.frame = viewFrame;
  
      keyboardShown = NO;
  }
  
  - (void)textFieldDidBeginEditing:(UITextField *)textField
  {
      activeField = textField;
  }
  
  - (void)textFieldDidEndEditing:(UITextField *)textField
  {
      activeField = nil;
  }
  </code></pre>