Search code examples
swifttableviewuisearchcontrolleruirefreshcontrol

TableView first cell hidden under search bar when returning to view


I have some weird behaviour on a TableView and hope someone could help me out.

I have a TableView with a searchbar in the navigation. The cells lead to a detail view. Whenever I search in the TableView and then hit a result cell to go to the detail view, on returning from the detail view to the TableView the first cell disappears underneath the opened search bar.

Searching:

Searching

On selecting the item (detail view) and then returning (using the nav bar) the first cell is hidden under the search bar:

Returning

Some things to note:

  • If I print the contentInset on returning it gives -44 (1 cell hidden, like in the pic). I'm unable to set the contentInset back to 0.0 (or anything else for that matter) as it doesn't seem to change anything.
tableView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
print(tableView.contentOffset.y)

// -44.0

If the first cell is hidden and I hide the search bar by pushing the table down and then up again, the problem automatically disappears and the ContentInset restores to 0.0.

UPDATE: I found that the view restores itself as well after returning the app from the background:

enter image description here

I can't however reproduce that in code. I've tried:

view.setNeedsLayout()
view.layoutIfNeeded()
tableView.reloadData()

Solution

  • Solved, I set searchController.hidesNavigationBarDuringPresentation = false, which caused the bug. See the answer on this post: Search Bar in a Navigation Item collapses and gets stuck under status bar upon navigation pop, on iOS 11.

    Thanks for the help!