I am adding UISearchController
searchBar to the controllers' view like this: self.view.addSubview(searchController.searchBar)
. The functionality is working perfectly fine except that upon selection of the tableview row the searchbar quickly moves down and reappears from the top. I tried the following things, none of which worked out:
tableView.tableHeaderView = searchController.searchBar
instead of directly adding to the viewsearchController.searchBar
to a separate view that I dragged to the controller setting up constraints on it. Tried clipping to bounds both the newly created view and the searchBar.UINavigationViewController
and setting self.navigationItem.titleView = searchController.searchBar
. I defined the frame of the searchBar, still nothing.Extend Edges
feature in the storyboard (Under top bars, etc.), but no selection worked outAdding lines (to viewDIdLoad
):
self.extendedLayoutIncludesOpaqueBars = true
self.definesPresentationContext = true
Any help would be greatly appreciated.
After trying all of the suggestion and searching over the internet, it caught my eye that the working examples of the UISearchController
implementation are done in UITableViewController
, but I had UIViewController
with UITableViewDataSource
and UITableViewDelegate
protocols on it. Unfortunately, due to app architecture I was not able to directly have UITableViewController
, so I needed to restructure the app, so that it had UINavigationController
where I embeded the searchBar
in navigationItem.titleView
(and not set it as tableView.tableHeaderView
like they always do in various tutorials since I needed the searchBar
to be fixed, not hidden when we do scrolling) and it worked. Here is how the ultimate working app architecture looks like:
The TrainingContainerViewController
has two Container View
s, in one we embed TrainingFilterTableViewController
that shows up the ultimate results of the autocomplete functionality (after clicking on an autocomplete row). Another Container View
embeds UINavigationController
(to the left) which, in turn, has TrainingSearchTableViewController
as its child.
The code that sets up the UISearchController
and its searchBar
is located in the TrainingSearchTableViewController
s' viewDidLoad
and is the following:
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
searchController.delegate = self
navigationItem.titleView = searchController.searchBar
tableView.hidden = true
...
}
Nothing else was needed to fix the bug in XCode 8.2.1
and Swift 2.3
, just change architecture, so that usage of UITableViewController
is possible in the app and use it instead of UIViewController
.