6
votes

I have implemented a UISearchController in a TableView, pushed by a Navigation Controller.

First my problem was that whenever I click on the SearchBar, it disappears. It works when I enter some text, but it stays completely blank. Then I managed to semi solve the issue using this code:

- (void)searchForText:(NSString*)searchText
{
    [self.view addSubview:villeSearchController.searchBar];
}

Which semi-works because now, when I click on the search bar, it blanks out, but if I enter one character, it appears again, and then it stays there, no matter what. Until I cancel the search, and click on it again, in that case it blanks out. I have made some tests and this method (searchForText) is called on the very first click, so that isn't the reason.

Does anyone know how I can solve this issue and make the searchbar appear from the very first click?

EDIT:

This is how I initialize the SearchController:

villeSearchController = [[UISearchController alloc]   initWithSearchResultsController:nil];
villeSearchController.searchResultsUpdater = self;
villeSearchController.dimsBackgroundDuringPresentation = NO;
villeSearchController.searchBar.delegate = self;
villeTableView.tableHeaderView = villeSearchController.searchBar;
villeSearchController.searchBar.scopeButtonTitles = @[];
self.definesPresentationContext = YES;
[villeSearchController.searchBar sizeToFit];
6
Hi. Please show your code for initialization of uisearchcontroller. Check this link and ensure that your initialization code is properly. https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.htmlSerge Maslyakov
Hi, I have updated the code. It looks fine to meel-flor
1) Try to check the NavigationBar a translucent property - it should be YES when SearchController will present the searchBar or else will be UI bugs. 2) If UISearchController used a default presentation you should not manually manage to adding searchBar to subView.Serge Maslyakov
Your first option works for me ! Please put it as an answer and I will accept it. The first option combined with the second! I don't add the subview anymore and just put the transluscent property!el-flor

6 Answers

15
votes

Try to check the navigationBar.translucent property - it should be YES when UISearchController will present the searchBar or else will be UI bugs.

Update from @SiavA

The better solution is use the extendedLayoutIncludesOpaqueBars property of the UIViewController. If you using the opaque navigation bar just set it in the true for controller which will be show UISearchController (not for navigationController).

E.g.

- (void)viewDidLoad {
    [super viewDidLoad];

    self.extendedLayoutIncludesOpaqueBars = !self.navigationController.navigationBar.translucent;
}
24
votes

This happened to me when the UISearchController was hiding the navigation bar. Setting this property fixed it:

UISearchController.hidesNavigationBarDuringPresentation = NO;

0
votes

Place the SearchController inside a UIScrollView and it will work fine. This if you are using it in the section header or as a separate view

0
votes

If you run into this problem in iOS11 (and especially if it worked pre iOS11), I had to change my UISearchController to be attached to the navigationItem rather than the tableView.

After setting parameters on my searchController, I used to do this:

tableView.tableHeaderView = searchController.searchBar

Now I have this:

navigationItem.searchController = searchController

The "translucent" fix would allow the controller to appear, but when I would try and unwind to a specific segue, I'd get a crash. Attaching the searchController to the navigationItem fixed both the display and the crash.

0
votes

Setting isHidden of the navigation bar to false stopped the search bar from disappearing for me.

      self.navigationController?.navigationBar.isHidden = false
0
votes

Hi Guys there is a very simple solution for the issue. -- this will solve major issues with collections of views and a parent view containing multiple viewcontrollers.

  • you don't need any below code if you have just remove it from your code

// searchController.definesPresentationContext = true

// self.definesPresentationContext = true

// self.extendedLayoutIncludesOpaqueBars = !(self.navigationController?.navigationBar.isTranslucent)!

I just add below code make sure you set searchController always set nil if you are switching between viewControllers there may be some conflicts that can be cleared by setting it searchController nil in the setupsearchbar(). also, disable the searchController on viewDidDissapear(), it will solve the issue related to active search bar moving on to the next screen.

var searchController = UISearchController(searchResultsController: nil)


func setupSearchBar() {

 searchController = UISearchController(searchResultsController: nil)

// adding search controller
searchController.searchResultsUpdater = self

// changing font color when user types
searchController.searchBar.searchTextField.textColor = .black

//allows select results from filtered table
searchController.searchBar.endEditing(true)

searchController.obscuresBackgroundDuringPresentation = false

searchController.hidesNavigationBarDuringPresentation = false
 
self.tableView.tableHeaderView = searchController.searchBar
 
    
}

override func viewDidDisappear(_ animated: Bool) {
    searchController.isActive = false

}