UISearchController隐藏了搜索栏上方的结果

7

我在UITableView的Container视图中使用了一个UISearchController。我是这样添加搜索栏的:

self.resultsTableController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([ResultViewController class])];

self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];

self.searchController.hidesNavigationBarDuringPresentation = NO;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.resultsTableController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = YES;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = NO;

请看以下截图。它会隐藏搜索栏上方的所有搜索结果:

未激活搜索栏时的原始图片。 搜索栏已激活并隐藏了所有上方元素。

请告诉我为什么会出现这种情况?
1个回答

1

我发现SearchController有点棘手。我遇到了很多问题,发现特定的顺序很重要。以下组合是对我有效的。

        searchController.searchResultsUpdater = self
        searchController.searchBar.delegate = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "search"
        searchController.searchBar.returnKeyType = .done
        searchController.searchBar.searchBarStyle = .minimal
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.searchBar.sizeToFit()
        definesPresentationContext = true
        if(tableView.tableHeaderView == nil){
            tableView.tableHeaderView = searchController.searchBar
        }

你可以拥有。
searchController.hidesNavigationBarDuringPresentation = false

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接