0

I have a view which has tableview on top and a search textfield (with a button) at bottom of tableview. I am trying to implement the dynamic search so that as and when user types the tableview is reloaded. The searchfield comes nicely on top of keyboard and does the search. But in some cases it goes below the keyboard while user is still typing. This typically happens when search returns 0 results which means tableview is empty or if tableview has rows which just fit the height allocated to tableview. I am not sure why does the tableview reload makes the search field go back to the bottom of the page even though the search field is not part of tableview.

Here is the code I use to move the search field based on keyboard state :

- (void)adjustView:(NSNotification *)notification {
    CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];    
    CGRect viewFrame = self.searchView.frame;
    viewFrame.origin.y = keyboardFrame.origin.y - viewFrame.size.height;
    self.searchView.frame = viewFrame;

}

Here searchView is the view containing the textfield and a button. Any help is greatly appreciated.

user1259574
  • 101
  • 1
  • 7
  • Is the textfield actually part of the tableview? Like in one of its cells? – Lyndsey Scott Dec 04 '14 at 18:38
  • no its is not. The main view has a tableview and a view which has 2 widgets - the textview and a button. – user1259574 Dec 04 '14 at 18:43
  • Could you post the code you use to move your UITextField up and down? – Lyndsey Scott Dec 04 '14 at 18:46
  • added the code which I use to move my UITextField. However please note that this method is not called when textfield goes does automatically. This is only called when I click on the textfield and want to type something or if I click done button in keyboard. – user1259574 Dec 04 '14 at 19:23
  • The issue's happening somewhere else in your code other than in the your `adjustView:` method. I explain why in my answer below. Is there anywhere else **at all** in your code (for example in `viewDidLayoutSubviews`) where you set `self.searchView.frame.height`? – Lyndsey Scott Dec 04 '14 at 20:40
  • Finally managed to solve this : tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; – user1259574 Dec 05 '14 at 17:49

1 Answers1

0

I'm not sure if this is exactly what you are looking for, but you could set the UITextField delegate and use these delegate methods. They are from my code, but they animate the view frame out of the way when the keyboard appears and then back when done.

#pragma mark - Text Editing functions

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CGRect viewFrame = self.frame;
    viewFrame.origin.y += animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    self.frame = viewFrame;

    [UIView commitAnimations];
}


- (void) textFieldDidBeginEditing:(UITextField*) textField {
    CGRect  textFieldRect   = [self.window convertRect:textField.bounds fromView:textField];
    CGRect  viewRect        = [self.window convertRect:self.bounds fromView:self];
    CGFloat midline         = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator       = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator     = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
    CGFloat heightFraction  = numerator / denominator;

    if (heightFraction < 0.0) {
        heightFraction = 0.0;
    } else if (heightFraction > 1.0) {
        heightFraction = 1.0;
    }

    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);

    CGRect viewFrame = self.frame;
    viewFrame.origin.y -= animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    self.frame = viewFrame;

    [UIView commitAnimations];
}
Larry Borsato
  • 394
  • 3
  • 5