Thursday, August 9, 2012

Scrolling UITableView (UITexField) withrespect to Keyboard

In the earlier post, I described about scrolling the UIView containing textfields  so that the keyboard will not hide textfields when it appears. Now, in this blog we gonna talk about the same case but with different scenario. This time i am assuming our uitextfied is inside the tableview cell and the following code ensures that those textfields will not hide behind the keyboard.
For this section , I am using NSNotificationCenter  to know whether keyboard is appearing or not , along with instead of scrolling the whole view  i am simply using the contentOffset property of tableview(UIScrollView).

1. Lets define custom method for registering the NSNotification
- (void)registerForKeyboardNotifications{
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:)
                      name:UIKeyboardDidShowNotification object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self    selector:@selector(keyboardWillBeHidden:)
                      name:UIKeyboardWillHideNotification object:nil];
  }

2. calling our custom registering nsnotification method when textfield is tapped
       (include textfield delegate in header file)
- (void)textFieldDidBeginEditing:(UITextField *)textField{
      [self registerForKeyboardNotifications];
}



3. Following method is called when UIKeyboardDidShowNotification is sent  (triggers when keyboard appears eg. from [textfield becomeFirstResponder] )
- (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);
      yourTableView.contentInset = contentInsets;
      yourTableView.scrollIndicatorInsets = contentInsets;

    // If currentSelected text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
       CGRect aRect = yourTableView.frame;
       aRect.size.height - = kbSize.height;

    if (currentSelectedTextField.tag < 3) {
      // this is your call where your textfield is? since for some of the texfields that is placed above the keyboard height we   don't need to change anything. Here, i am assuming for the textfields having tag<3, we don't change  tableview offsets
        CGPoint scrollPoint = CGPointMake(0.0, 0.0);
        [yourTableView setContentOffset:scrollPoint animated:YES];
    }
    else {
        CGPoint scrollPoint = CGPointMake(0.0, currentSelectedTextField.frame.origin.y + kbSize.height -55); //55 : change it on your requirement to adjust view frame
        [yourTableView setContentOffset:scrollPoint animated:YES];
    }

}

4. Following method is called when UIKeyboardWillHideNotification is sent  (triggers when keyboard dissapears eg. from [textfield resignFirstResponder] )
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
      UIEdgeInsets contentInsets = UIEdgeInsetsZero;
      yourTableView.contentInset = contentInsets;
      yourTableView.scrollIndicatorInsets = contentInsets;
   }

2 comments: