iOS 11上的UITableView中的UISearchController问题

问题描述 投票:2回答:1

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 objective-c uitableview ios11 uisearchcontroller
1个回答
2
投票

在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];
}
© www.soinside.com 2019 - 2024. All rights reserved.