UISearchController将结果隐藏在搜索栏上方

时间:2015-03-31 20:09:29

标签: uitableview ios8 uisearchcontroller childviewcontroller childviews

我在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;

请参阅以下屏幕截图。它隐藏了搜索栏上方的所有搜索结果:

original image when searchbar is not active. SearchBar active and all above elements get hidden.

请帮我解释为什么我会这样做?

1 个答案:

答案 0 :(得分:0)

我发现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