How to stop UISearchBar from Scrolling with UITableview in iOS 8

3.7k views Asked by At

I'm using UISearchBar , UISearchController and UISearchResultsController and can't figure out, how to stop UISearchBar from Scrolling with UITableview in iOS 8

Didn't find the answer that works.

2

There are 2 answers

0
T'Pol On BEST ANSWER

Please see Benjamin's excellent response here: https://stackoverflow.com/a/27129481/1505096

In short: Use a UIViewController with an embedded UITableView, not a UITableViewController. The former gives you the flexibility to do whatever you want with the table view, including the oft-requested behavior you are seeking, while in the latter the view itself is a table view, so you're much more limited in what you can do outside of the normal design paradigm.

Playing with UIScrollViewDelegate and adjusting frames therein--as I've seen in many SO responses--is a hack, and you don't need to resort to that chicanery.

3
Francis On
  1. Use a UIViewController instead of UITableViewController
  2. Add a subview just below the status bar, that's the size of UISearchBar
  3. Add a tableView right below the subview
  4. Connect your outlets, dataSource, and delegate
  5. Use AutoLayout, to keep the views in position
  6. In the viewDidLoad method, configure your UISearchController as usual, but instead of setting the searchController searchBar as the tableView header, add it as a subview of the view you made in storyBoard
  7. That's it... Just posting in case anyone else's interested.