I have two scenes which can be accessed through a tab bar, on scene 1 there is a search bar. the problem that I am facing is that while searching if I switch to the download
Behind the scenes the search controller is being presented. This is what is causing the black screen when returning.
The simplest way is to override UISearchController and set isActive=false
in it's viewDidDisappear
(https://stackoverflow.com/a/39212080/215748). It works, but I found some usability issues with this solution. I didn't pursue it, so they may be easy enough to overcome.
Call the following before moving away from the view controller:
searchController.dismiss(animated: false, completion: nil)
Add this to viewDidDisappear
.
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
dismissSearch()
}
func dismissSearch() {
searchController.dismiss(animated: false, completion: nil)
}
Unfortunately, viewDidDisappear
doesn't get called when you switch tabs, because the search controller is being presented. It received the viewDidDisappear
. To get around this, you can subclass UITabBarController
and implement UITabBarControllerDelegate
.
// don't forget to set the delegate
extension TabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let navigationController = viewControllers?[selectedIndex] as? UINavigationController {
for myController in navigationController.viewControllers.flatMap({ $0 as? MyTableViewController }) {
myController.dismissSearch()
}
}
return true
}