I have code that creates a UISearchController\' in my UIVIew\'s
viewDidLoad`.
self.resultSearchController = ({
let controller = UISearch
Creating a search controller in viewDidLoad()
and setting its search bar as the navigation item's title view doesn't create a strong reference to the search controller, which is why it's deallocated.
So instead of doing this:
override func viewDidLoad() {
super.viewDidLoad()
// Create search controller
let searchController = UISearchController(searchResultsController: nil)
// Add search bar to navigation bar
navigationItem.titleView = searchController.searchBar
// Size search bar
searchController.searchBar.sizeToFit()
}
You should do this:
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
// Create search controller
searchController = UISearchController(searchResultsController: nil)
// Add search bar to navigation bar
navigationItem.titleView = searchController.searchBar
// Size search bar
searchController.searchBar.sizeToFit()
}