Search code examples
iosuitableviewuiviewcontrollerswift3uisearchcontroller

UISearchController incorrect behavior when it is present modally


I showed UIViewController like modal style. UIViewController has UITableView. I added UISearchController to UITableView but UISearchController has incorrect behavior when it disappeared. I made many times UISearchControllers but I didn't face the this behavior.

My code

So I show the UIViewController

 @objc fileprivate func selectInterlocutor(_ button: UIButton) {
        let selectInterlocutorTVC = SelectInterlocutorTableViewController()
        selectInterlocutorTVC.modalPresentationStyle = .overCurrentContext
        selectInterlocutorTVC.modalTransitionStyle = .coverVertical
        selectInterlocutorTVC.providesPresentationContextTransitionStyle = true
        present(selectInterlocutorTVC, animated: true) {

        }
    }

I add UISearchController to tableView

  fileprivate func addSearchController() {
        searchController.searchBar.barStyle = .default
        searchController.delegate = self
        searchController.searchBar.delegate = self
        searchController.searchResultsUpdater = self
        searchController.searchBar.autocapitalizationType = .words
        searchController.dimsBackgroundDuringPresentation = false
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.searchBar.sizeToFit()

        tableView.tableHeaderView = searchController.searchBar
    }

Please look at this video that you can see this behavior. https://www.dropbox.com/s/l3m3q8wmqoy3qv2/SearchBarBug.mov?dl=0

How can I fix it?


Solution

  • I removed UISearchController and I used UISearchBar and it works for me.

     fileprivate func addSearchController() {
            searchBar.barStyle = .default
            searchBar.delegate = self
            searchBar.autocapitalizationType = .words
            searchBar.showsCancelButton = true
            searchBar.sizeToFit()
    
            tableView.tableHeaderView = searchBar
        }