ios - iOS 11 上 UITableView 中的 UISearchController 问题

标签 ios objective-c uitableview ios11 uisearchcontroller

iOS 11 我使用以下代码在表格的 headerview 上有一个 UITableViewControllerUISearchController :

if(searchController == nil)
{
    searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    searchController.dimsBackgroundDuringPresentation = false;
    searchController.searchResultsUpdater = self;
    searchController.searchBar.delegate = self;
    self.definesPresentationContext = NO;
    searchController.delegate = self;
}
self.tableView.tableHeaderView = searchController.searchBar;

[self.tableView setContentOffset:CGPointMake(0,searchController.searchBar.bounds.size.height) animated:YES];

当我运行应用程序时,我想要的行为对于行数超过设备屏幕高度的 tableview 来说是完美的,但在 tableview 具有的情况下只有几行,搜索栏从导航栏下降了一点。

When there are few cells on the tableview, the search bar drops down as in this picture

When there are more cells on the tableview, the search bar doesn't drop down

最佳答案

在 iOS 11 上,带有 tableView header 的 UISearchController 的行为已更改,现在它是 navigationItem 的一部分,并且您不需要 tableView。因此,您必须检查 iOS >= 11 并执行以下操作

if (@available(iOS 11.0, *)) {
    self.navigationItem.searchController = searchController;
// ToDo : dont forget to hide the tableView 
// ...       
} else {
    self.tableView.tableHeaderView = searchController.searchBar;
    [self.tableView setContentOffset:CGPointMake(0,searchController.searchBar.bounds.size.height) animated:YES];
}

关于ios - iOS 11 上 UITableView 中的 UISearchController 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48007690/

相关文章:

iphone - 在 ios5 中设置色调颜色效果很好,但在 ios4 中会崩溃

iphone - 将HTTP响应转换为字符串后,EXC_BAD_ACCESS

objective-c - 来自网络摄像头的 cocoa 捕获帧

ios - 我如何在同一个 xib 中使用自定义 UITableViewCell 和 UITableView

iphone - 如何在 uitableviewcell 中将文本字段辞去第一响应者

ios - 从 UIFont 获取 Black Italic 系统字体

iphone - 如何弄清楚使用的 SEL 名称是什么

ios - 如何将变量从 flutter 传递到 native objective-c

ios - 如何判断 'Mobile Network Data' 在 iOS 中是启用还是禁用(即使通过 WiFi 连接)?

iphone - 分组 UITableView 和编辑模式 : strange behaviour