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
AFTER SEARCH
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?