Tab Bar and UISearchController giving black screen

后端 未结 6 842
温柔的废话
温柔的废话 2021-01-07 20:36

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

6条回答
  •  一生所求
    2021-01-07 20:59

    Behind the scenes the search controller is being presented. This is what is causing the black screen when returning.

    Solution 1

    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.

    Solution 2

    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
        }
    

提交回复
热议问题