So i have a searchcontroller on my navigationItem.
// View controller
var searchController = UISearchController(searchResultsController: nil)
override func viewWillAppear(_ animated: Bool) {
tableView.reloadData()
if #available(iOS 11.0, *) {
navigationController?.navigationBar.prefersLargeTitles = true
}
if employeeSearchList.count > 10 {
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
}
} else {
if #available(iOS 11.0, *) {
let search = UISearchController(searchResultsController: nil)
navigationItem.searchController = search
navigationItem.searchController = nil
} else {
tableView.tableHeaderView = nil
}
}
UIView.animate(withDuration: 0.50, animations: {
self.view.layoutIfNeeded()
})
}
This piece of code runs perfectly. If the count is less than 10, the search controller is set, otherwise it is set to an empty search controller and then set to nil
, so that it disappears from the UI.
func viewDidLoad() {
.
.
.
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.tintColor = tintColor
// Table view editing delegate -> delete operation
if employeeSearchList.count < 10 {
if #available(iOS 11.0, *) {
let search = UISearchController(searchResultsController: nil)
navigationItem.searchController = search
navigationItem.searchController = nil
} else {
tableView.tableHeaderView = nil
}
UIView.animate(withDuration: 0.50, animations: {
tableView.reloadData()
self.view.layoutIfNeeded()
})
}
.
.
.
}
Now my problem is, when i present a view controller above this one and then dismiss it, the viewWillAppear
executes fine but the search controller doesn't show up. But if i push the view controller and come back, it shows up.
What are the main difference between the 2 operations ? (push/pop & present/dismiss)
It should be:
navigationController.navigationItem.searchController = searchController
instead of:
navigationItem.searchController = searchController
The latter will only take effect the next time navigationController is refreshed/loaded/whatever the appropriate term is?