Tab Bar and UISearchController giving black screen

后端 未结 6 839
温柔的废话
温柔的废话 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
        }
    
    0 讨论(0)
  • 2021-01-07 21:08

    Just add UiNavigationcontroller to uiviewcontroller of download tab it will resolve both issue blackout and navigation bar hidden

    0 讨论(0)
  • 2021-01-07 21:10

    This is because there is probably no separate navigation controller for the tab that gives you a black screen on back. For maintaining the navigation hierarchy seperately, you should embed a uinavigationcontroller to the uiviewcontroller and from IB check Top Bar as opaque navigation bar instead of Inferred. Hope this helps. Cheerio

    0 讨论(0)
  • 2021-01-07 21:10

    What if you move this bit of code

    self.activityIndicator.stopAnimating()
    self.activityIndicator.hidden = true
    

    to after your result? Like this:

              // If the network works fine
              if response.result.isFailure != true {
    
                   self.activityIndicator.stopAnimating()
                   self.activityIndicator.hidden = true
    

    (if that works, you would also need to include it on the other side of your else too... )

     // If the network fails
                    else {
                        self.activityIndicator.stopAnimating()
                        self.activityIndicator.hidden = true
    
                        self.retryButton.hidden = false
                        self.loadingMessageLabel.text = "Check your internet connectivity"
                    }
    
    0 讨论(0)
  • 2021-01-07 21:16

    I had this problem in iOS12 and fixed it by setting definesPresentationContext to true for the viewcontroller hosting the search controller.

    self.definesPresentationContext = true
    
    0 讨论(0)
  • Looks like the view that your UISearchController is attached to gets removed from the view hierarchy. You can think of the UISearchController as being presented modally when you start searching, and the definesPresentationContext property indicates which UIViewController would be the one to present it (more on this).

    You can get more detail in the answer to a possibly duplicate question: https://stackoverflow.com/a/37357242/300131

    0 讨论(0)
提交回复
热议问题