27

以下问题仅发生在 iOS 7.0+ 设备上运行的 iOS 6.0/6.1 应用程序上。

所以我有一个UISearchDisplayController搜索我们的 API 并返回数据的方法。这一切都有效,一切都按照我们的意愿显示。我们看到的唯一问题是,在内容填充 之后searchResultsTableView,看起来好像最初隐藏键盘时, 的contentSizesearchResultsTableView数据大得多,实际上似乎是键盘的大小。当我进入搜索栏,显示键盘并再次点击“搜索”时(只是为了隐藏键盘),contentSize然后正确调整为仅填充屏幕,仅此而已。下面是我正在谈论的初始tableView人口的屏幕截图。

白色是表格数据,灰色/奶油色是多余的tableView空间。

有想法该怎么解决这个吗?

4

2 回答 2

62

我有这个确切的问题。在此处的开发人员论坛上发布的解决方案对我有用。不确定这是否是 iOS 7 中的错误,或者只是他们改变了他们做事的方式,但这是我发现解决我的问题的唯一解决方案。

论坛帖子中针对懒人的解决方案:

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

}
于 2013-10-03T14:46:40.353 回答
8

这个系统错误仍然存​​在于 iOS 8 中,并且接受答案的解决方案不再起作用。因此,您应该使用以下解决方案:

-(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:(NSNotification*)notification {
    CGFloat height = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    UITableView *tableView = [[self searchDisplayController] searchResultsTableView];
    UIEdgeInsets inset;
    [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ? (inset = UIEdgeInsetsMake(0, 0, height, 0)) : (inset = UIEdgeInsetsZero);
    [tableView setContentInset:inset];
    [tableView setScrollIndicatorInsets:inset];
}
于 2015-03-09T08:34:28.863 回答