简体   繁体   中英

Issue with search textfield and tableview

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.

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM