iOS, Swift 3 - UISearchBar disappears when I click on Cancel after returning from Detail View

这一生的挚爱 提交于 2019-12-11 05:44:16

问题


I have a tableView in a containerView. Programmatically added a searchBar to it. Everything works fine, except for the case: When I tap on a cell, while the tableView is filtered by the searchBar, and then I return from the detailView (that was presented via push segue) and then I dismiss the searchBar (cancel button), then the searchBar disappears. Mysteriously, when I debug it on the console, the searchBar object is still there and it is still the headerView of the tableView... Anybody has an idea, what could cause this problem, and how to fix it?

Here is my relevant code:

In viewDidLoad:

self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.definesPresentationContext = false
self.tableView.tableHeaderView = self.searchController.searchBar

searchControllerDelegate:

func willPresentSearchController(_ searchController: UISearchController) {
    if let mpvc = self.parent as? MyPulleyViewController {
        mpvc.navigationController?.navigationBar.isTranslucent = true
    }
}


func willDismissSearchController(_ searchController: UISearchController) {
    if let mpvc = self.parent as? MyPulleyViewController {
        mpvc.navigationController?.navigationBar.isTranslucent = false
    }
}

(myPulleyViewController is the VC containing the containerView, self is the containerView's VC)

On the IB, the mpvc is set to Extend edges: Under Opaque Bars

Thanks for any help!


回答1:


I faced the same problem, I think this is iOS issue, I fixed it by making viewcontroller for Search Result :

   let searchVC = mainStoryboard.instantiateViewController(withIdentifier: identifier) as! SearchResultViewController
    let searchController = UISearchController(searchResultsController: searchVC)
    searchController.searchResultsUpdater = searchVC

and it works fine.



来源:https://stackoverflow.com/questions/45550526/ios-swift-3-uisearchbar-disappears-when-i-click-on-cancel-after-returning-fro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!