iOS 11 我使用以下代码在表格的 headerview 上有一个 UITableViewController
和 UISearchController
:
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/