UISearchDisplayController 搜索后隐藏 searchBar [英] UISearchDisplayController hide searchBar after searching

查看:24
本文介绍了UISearchDisplayController 搜索后隐藏 searchBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UISearchDisplayController 从我的列表中进行搜索.但是当我搜索一个字符时,它隐藏了 searchBar.这是我开始搜索之前的快照:

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:

这里隐藏了 searchBar 像这样:

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

        }
    }
}

推荐答案

我必须为 searchResultsTableView 设置框架,因为它的原点是 (0,0).

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

解决了这个问题:

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

默认情况下,UISearchDisplayControllersearchResultsTableView 的框架设置为 (0,0),因此 searchBar 隐藏在 后面searchResultsTableView.

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

这篇关于UISearchDisplayController 搜索后隐藏 searchBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆