Search code examples
iosswiftuinavigationbaruisearchbaruisearchcontroller

UISearchController's UISearchBar shifts incorrectly and jumps when UITableView is scrolled


In the video, it's a little difficult to see what's going on, so I'll try to explain it. https://youtu.be/yOrCJB9yZlg

I have UIViewController with UITableView inside its root view. Binded via Autolayout (there is no difference how it's binded: to SuperView or to SafeArea).

SearchBar added like this: let search = UISearchController(searchResultsController: nil) self.navigationItem.searchController = search

When I slowly scroll table, the transition between large navbar and compact navbar, and then animation of showing in and out of searchBar is too fast. Searchbar jumping in/out of navbar instead of smoothly opened/closed; navbar transitioning is jumping between two states, large and compact, without smoothly passes through the middle half-opened state while you slowly moving your finger on screen, like in system apps (Mail, Phone, Messages, Contacts etc).

I made the example from an empty project to demonstrate the issue; there isn't any changes to navigation bar logic, or any logic at all. Just two new VC's and this odd behaviour.

If i create xCode's "Master-Details" project example and add UISearchController to it, it will work properly. I assume its because they used UITableViewController instead of UIViewController + UITableView inside.

What the reason of this behaviour and how to fix it?


Solution

  • What you have done is correct. Did you try running your code in a device? Feels more like a glitch in the simulator. I tried what you tried and it works fine for me in the device. Whereas the glitch occurs in simulator.

    Refer to this article. They have explained step by step process.