I have successfully get UISearchController to work in a new project, but when I am trying to do the same thing in my own project, it behaves differently.
This is what I have done to get it to work.
Storyboard:
This is my code for my view controller:
class ViewController: UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let modalVC = mainStoryboard.instantiateViewController(withIdentifier: "SearchTableViewController_SBID")
self.searchController = UISearchController(searchResultsController: modalVC)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchController.searchBar
self.definesPresentationContext = true
}
func updateSearchResults(for searchController: UISearchController) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The result I am getting is, when I click on the search bar, the background will be dimmed, and when I started typing, the tableview controller will be visible, will the search bar is still visible in the navigation bar in the tableview controller.
However, when I try to implement the same thing in my project, when I started typing on the search bar, the table view controller will fill the whole screen, and the navigation bar is not visible.
This is the storyboard for my project:
Notice both projects are similar, except for one of them is Navigation Controller -> Tab Bar View Controller
, which doesn't show the navigation bar when I type on the searchbar.
How do I get the navigation bar to show in a project Navigation Controller -> Tab Bar View Controller
?
This is what it looks like for 2 projects: