UISearchController searchBar disappears on first click
Asked Answered
E

6

6

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];
Eton answered 26/10, 2015 at 8:37 Comment(4)
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.htmlKellda
Hi, I have updated the code. It looks fine to meEton
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.Kellda
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!Eton
K
16

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;
}
Kellda answered 26/10, 2015 at 15:46 Comment(3)
OH MY GOD THANK YOU SOOO MUCH!Poon
It's not UI bug. If navigationBar.transcluent is NO, then your navigation bar is opaque and u have to notify UI about it corresponding to iOS 7 Using View Controllers (also take a look at edgesForExtendedLayout): developer.apple.com/library/ios/documentation/UserExperience/… So, to fix your "bug" u have to set extendedLayoutIncludesOpaqueBars to YES for your navigation controller.Leukemia
@SiavA. Hi. A nice comment. I will add it to the response.Kellda
K
24

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

UISearchController.hidesNavigationBarDuringPresentation = NO;

Kaltman answered 5/2, 2016 at 14:24 Comment(1)
This is the correct answer. It even works for iOS 11.Nyhagen
K
16

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;
}
Kellda answered 26/10, 2015 at 15:46 Comment(3)
OH MY GOD THANK YOU SOOO MUCH!Poon
It's not UI bug. If navigationBar.transcluent is NO, then your navigation bar is opaque and u have to notify UI about it corresponding to iOS 7 Using View Controllers (also take a look at edgesForExtendedLayout): developer.apple.com/library/ios/documentation/UserExperience/… So, to fix your "bug" u have to set extendedLayoutIncludesOpaqueBars to YES for your navigation controller.Leukemia
@SiavA. Hi. A nice comment. I will add it to the response.Kellda
B
0

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

Buoyancy answered 24/8, 2016 at 8:35 Comment(0)
H
0

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.

Halflength answered 15/11, 2017 at 21:42 Comment(0)
A
0

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

      self.navigationController?.navigationBar.isHidden = false
Anamorphism answered 26/4, 2018 at 9:41 Comment(0)
S
0

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

}
Straightout answered 16/10, 2020 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.