UISearchDisplayController hide searchBar after searching

好久不见. 提交于 2019-12-11 09:45:47

问题


I am using UISearchDisplayController to search from my list. But when I search a character, it hides searchBar. Here is the snapshot before I start searching:

Here it hides searchBar like this:

#pragma mark - UISearchDisplayController Delegate Methods
//These methods will be used for search controller frame
- (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];
}

- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {

}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.001);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        for (UIView* v in self.searchDisplayController.searchResultsTableView.subviews) {
            if ([v isKindOfClass: [UILabel class]] &&
                [[(UILabel*)v text] isEqualToString:@"No Results"]) {
                [(UILabel *)v setText:NO_RESULT_LABEL_STRING];
                // .. do whatever you like to the UILabel here ..
                break;
            }
        }
    });

    [self handleSearchForTerm:searchString];
    return YES;
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    [[self dealsTableView] reloadData];
}

#pragma mark Search Method

- (void) handleSearchForTerm:(NSString *) searchString {

    //First get all the objects which fulfill the criteria

    [[self searchResultsArray] removeAllObjects];
    for (Deal *currentDeal in self.dealsArray)
    {
        NSArray *searchQueryComponents = [searchString componentsSeparatedByString:@" "];

        BOOL isGoAheadToAdd=YES;

        for (NSString *currentSearchComponent in searchQueryComponents) {

            NSString *trimmed = [currentSearchComponent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

            if ([trimmed length]>0) {
                if ([[currentDeal title] rangeOfString:currentSearchComponent options:NSCaseInsensitiveSearch].location != NSNotFound)
                {
                    isGoAheadToAdd=YES;
                }
                else {
                    isGoAheadToAdd=NO;
                    break;
                }
            }
        }

        if (isGoAheadToAdd) {
            [[self searchResultsArray] addObject:currentDeal];

        }
    }
}

回答1:


try this

@interface MySearchDisplayController : UISearchDisplayController

@end

@implementation MySearchDisplayController

- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
    [super setActive: visible animated: animated];

    [self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];
}



回答2:


I had to set the frame for searchResultsTableView because its origin was at (0,0).

Solved it by this:

-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
    [tableView setFrame:CGRectMake(0, 42, 320, 380)];
}

By default, UISearchDisplayController sets the frame for searchResultsTableView at (0,0), so the searchBar was hiding behind the searchResultsTableView.



来源:https://stackoverflow.com/questions/24749899/uisearchdisplaycontroller-hide-searchbar-after-searching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!