1

I have a text box in a TableView and when the user selects a switch it appears ( becomes visible) and a keyboard pops up, and when the user turns off the switch the TextBox disappears and keyboard hides. During the time when the keyboard appears and disappears I want to shift the tableView slightly up and then back to original position. Here is the code

-(IBAction)actionSwitch:(id)sender
{
isSearchTerm = [switchSearchTerm isOn];
[self.tableView reloadData];
if(isSearchTerm == YES)
{
    [txtSearchTerm becomeFirstResponder];
    floatBottom = self.tableView.contentInset.bottom;

    self.tableView.contentInset=UIEdgeInsetsMake(0,0,200,0);
    [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForSelectedRow] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
else
{
    [txtSearchTerm resignFirstResponder];
    self.tableView.contentInset=UIEdgeInsetsMake(0,0,floatBottom,0);
    [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForSelectedRow] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}

The else part doesn't bring the tableview back to its original position, please help.

John Powell
  • 12,253
  • 6
  • 59
  • 67
vishal dharankar
  • 7,536
  • 7
  • 57
  • 93

2 Answers2

3

If your tableView lies in a navigation controller or the rootViewController of a navigation controller and you are doing all this on iOS7+ with UIViewController's property automaticallyAdjustsScrollViewInsets set to YES. You'd better handle the keyboard appear as the document suggested

// 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 app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

There is another way to do this. Both can be found in "Moving Content That Is Located Under the Keyboard" "Text Programming Guide for iOS"

dopcn
  • 4,218
  • 3
  • 23
  • 32
0

I solved this by setting the extend under top bar property to NO , under view controllers properties in Property inspector by selecting the ViewController.

vishal dharankar
  • 7,536
  • 7
  • 57
  • 93