UISearchController makes the controller black
Asked Answered
G

2

11

I am using UISearchController in iOS 8 with the following intializaiton in viewDidLoad of a view controller embedded in a tab controller

 _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];

    _searchBar = _searchController.searchBar;
    [_searchController.searchBar sizeToFit];
    _searchController.searchBar.delegate = self;
   _searchController.searchResultsUpdater = self;
    _searchController.dimsBackgroundDuringPresentation = NO;
    _searchController.hidesNavigationBarDuringPresentation  = NO;
    self.definesPresentationContext = NO;
    _shopsTable.tableHeaderView = _searchController.searchBar;

I've implemented

- (void) updateSearchResultsForSearchController:(UISearchController *)searchController and (void)filterContentForSearchText:(NSString *)searchText

and the search works, the tableview gets updated properly, etc.

But!

If I switch tabs while the searchcontroller is active (just tapping the search bar or with some text) to a different tab, and then go back to teh search tab, I get a blank screen with only the search bar, like this

enter image description here

In this case, I search for things that start with lar, which did return results and displayed them correcly. But if I switch tabs, and return to the search tab I get a blank screen like this. The only way the controller returns to its original state is if I do _searchController.active = NO. But if the user wants to keep that search active, I can't just deactivate it.

I am sure I am missing something, but since there isn't much to do in UISeachController, I can't figure out what is causing this..

Glennaglennie answered 23/2, 2015 at 14:24 Comment(1)
can you please provide sample objective c code, i am stuck with same issue? I am very new to IOS development. I have separate view controller for search as well as main list. is this cause the problem.Nissa
P
22

Try self.definesPresentationContext = YES; instead of NO. Here is how I setup my UISearchController, but I haven't done it this way in a UITabBarController before.

func setupSearchController() {
    let resultsController = UIStoryboard(name: "ATPageTableViewController", bundle: nil).instantiateViewControllerWithIdentifier("ATPageTableSearchResultsViewController") as! ATPageTableSearchResultsViewController
    searchController = UISearchController(searchResultsController: resultsController)
    searchController.delegate = self
    resultsController.delegate = self
    resultsController.cellIdentifier = ATDataSetItemTableViewCellIdentifier;
    resultsController.table = self.table!
    searchController.searchBar.sizeToFit()
    self.tableView.tableHeaderView = searchController.searchBar
    searchController.searchResultsUpdater = self
    searchController.searchBar.delegate = self
    definesPresentationContext = true

}
Puggree answered 23/2, 2015 at 14:26 Comment(2)
Thanks, it didnt directly help me but it led me to the problem. I used a view controller with table delegates, and once I switched to a tableview controller instead, the problem got resolved..Glennaglennie
this line did it for me: definesPresentationContext = trueBoyse
S
0

Write the below code in viewDidLoad mehtod.

self.definesPresentationContext = true

searchController.searchResultsUpdater = self

searchController.searchBar.delegate = self

searchController.dimsBackgroundDuringPresentation = false searchController.definesPresentationContext = true

Suture answered 9/6, 2017 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.