I want implement an UISearchBar
on my tableView
.
My code (in viewDidLoad) :
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.searchBar.autocapitalizationType = .None
self.searchController.searchBar.autocorrectionType = .No
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = false
self.tableView.tableHeaderView = self.searchController.searchBar
When I click on the searchBar
, this one move to top, like it wants hide the navigationBar
:
I searched on many posts for an answer but nothing works. I want to disable this animation so that the searchBar doesn't move.
You can think of the
UISearchController
as being presented modally when you start searching. This would work fine if you had a usualUINavigationController
setup, however in a more "customized" UI like yours you may run into issues like the search controller attaching to a wrong view, etc.I would suggest not to use
UISearchController
in your case and use a separateUISearchBar
initialised with the rest of your interface (probably in a storyboard?), and then do the search manually. Implement theUISearchBarDelegate
and add your ownUITableView
for the search results, if you can't simply filter your content in place.Although looks like you have a
tableView
var — you could simply add another property, similar to the one you use as aUITableViewDataSource
and store filtered data there. So whenever you have something in theUISearchBar
you simply use a filtered data source when reloading table view data. For instance:And then use
filteredData
in theUITableViewDataSource
:And then do something like this in the
UISearchBarDelegate
:Paste this line in your viewDidLoad of presenting controller:
self.extendedLayoutIncludesOpaqueBars = true
You should be able to prevent this by setting: