0

This is something I've done before that has worked perfectly, so I'm puzzled as to why it's not functioning correctly now.

What I'm wanting to do is have a search bar in the header of my table view that doesn't move when it becomes active. I've achieved this in another view using the same tableView.tableHeaderView method (the one difference between the two is that the working searchController is in a TableViewController embedded in a NavigationController instead of in a regular ViewController with a table).

I'll get to the code momentarily, here are the pictures.

Incorrect behavior: BEFORE SEARCH

Before entering search criteria

AFTER SEARCH

After entering search criteria

As you can see, in the second image, when the searchController becomes active, it jumps up to the top of the view instead of staying in the headerView like I think it should. It also leaves a really ugly gap at the top of the tableView where the header is. I made a prior version of my app about a year ago where the search functionality on this screen worked perfectly, and using the same exact code from that project still results in this weird behavior where the searchController leaves the tableView.

In terms of my code, I've made my class conform to UISearchResultsUpdating and have implemented the required updateSearchResults(for:) function.

I declared my variable in my class:

var searchController: UISearchController!

And then in my viewDidLoad, I do the following:

    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.barTintColor = .white
    searchController.searchBar.backgroundImage = UIImage()
    searchController.searchBar.tintColor = GlobalPropertyKeys.LovalyticsBlue
    searchController.hidesNavigationBarDuringPresentation = false
    allListItemsTableView.tableHeaderView = searchController.searchBar

Which is the same code that works in my other viewController (granted, the other one is a tableViewController, but I had also made an @IBOutlet for the tableView there and attached the searchController to the tableHeaderView). It's probably worth mentioning that those radio buttons at the top trigger my tableView to display different arrays. In the old version of my app, it was really cool because toggling those radio buttons while the searchController was active would display the search results for each array automatically.

Am I missing something incredibly obvious here?

MachTurtle
  • 210
  • 3
  • 10
  • Have you tried any of the solutions described here: https://stackoverflow.com/questions/28326269/uisearchbar-presented-by-uisearchcontroller-in-table-header-view-animates-too-fa? – crizzis Apr 18 '18 at 21:19
  • @crizzis Wow looks like this is a problem that's been plaguing some people for a while, thanks for the link. I went through every answer to see if it solved my issue, but no dice unfortunately. I tried checking Under Opaque Bars in my view controller, setting extendedLayoutIncludesOpaqueBars to true, definesPresentationContext = true, searchBar.scopeButtonTitles = [], edgesForExtendedLayout = .none, checking Under Top Bars in my view controller, setting the navigation bar to translucent, setting clipsToBounds = true, and edgesForExtendedLayout = .bottom and pretty much every combination. – MachTurtle Apr 19 '18 at 18:04

0 Answers0