Attempting to load the view of a view controller while it is deallocating... UISearchController Attempting to load the view of a view controller while it is deallocating... UISearchController ios ios

Attempting to load the view of a view controller while it is deallocating... UISearchController


UISearchController's view has to be removed from its superview before deallocate. (guess it is a bug)

Objective-C...

-(void)dealloc {     [searchController.view removeFromSuperview]; // It works!}

Swift 3...

deinit {    self.searchController.view.removeFromSuperview()}

I struggled with this issue for a couple of weeks. ^^


Solved! It was a simple fix. I changed this code

class ViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {    var resultSearchController = UISearchController()

to this:

 class ViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {    var resultSearchController: UISearchController!

This fixes the problem.


Here is the Swift version that worked for me (similar toJJHs answer):

deinit{    if let superView = resultSearchController.view.superview    {        superView.removeFromSuperview()    }}