Search code examples
iosobjective-cios7uisearchbaruisearchdisplaycontroller

UISearchBar disappears when I tap second time


I have app with UIViewController which contains UISegmentView, UISearchDisplayController and UITableView like at picture:

enter image description here

When I tap into search bar, everything is fine. Keyboard appears and I can do my search activity. But after I tap into search bar second time, It disappears - on second picture:

enter image description here

When I add:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
self.searchDisplayController.searchResultsTableView.frame = CGRectMake(0, 20,     self.tableView.frame.size.width, self.tableView.frame.size.height);
}

I can see search bar under searchResultsTableView:

enter image description here

Could anybody help me? Thank you!


Solution

  • Ok, I finally have the solution, but simultaneously I know that it isn't the best solution.

    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
        CGRect newFrame = [self.view.superview convertRect:self.searchDisplayController.searchResultsTableView.superview.superview.frame toView:nil];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
        self.searchDisplayController.searchResultsTableView.superview.superview.frame = CGRectMake(0, -40, self.tableView.frame.size.width, window.bounds.size.height - newFrame.origin.y);
        return YES;
    }
    

    and

    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
        CGRect newFrame = [self.view.superview convertRect:self.searchDisplayController.searchResultsTableView.superview.superview.frame toView:nil];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
        self.searchDisplayController.searchResultsTableView.superview.superview.frame = CGRectMake(0, 4, self.tableView.frame.size.width, window.bounds.size.height - newFrame.origin.y);
    
        return YES;
    }
    

    and finally for content size:

    - (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    }
    
    
    
    - (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
    }
    
    - (void) keyboardWillHide {
        UITableView *tableView = [[self searchDisplayController] searchResultsTableView];
    
        [tableView setContentInset:UIEdgeInsetsZero];
        [tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
    }