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标头的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];
}