How to animate alongside a UISearchController presentation/dismissal animation?

前端 未结 1 1197
故里飘歌
故里飘歌 2021-01-11 16:52

I have a table view with a search bar in the tableHeaderView, managed by a UISearchController. I use the standard UISearchController p

相关标签:
1条回答
  • 2021-01-11 17:18

    I had the same problem, I needed to animate other views alongside the the presentation of the UISearchController; After the call to present the search controller the transitionCoordinator becomes available and you can add code to animate your views

    Presenting:

    func search() {
        let searchController = UISearchController(searchResultsController: resultsController)
        // Configure search controller
        self.present(searchController, animated: true) {}
    
        self.transitionCoordinator?.animate(alongsideTransition: { (context) in
            // animate other views
        }, completion: nil)
    }
    

    I also had to animate the views while dismissing the search controller, in this case I implement the willDismissSearchController method of the UISearchControllerDelegate, the transitionCoordinator is not immediately available in this method but making an asynchronous call does the trick

    Dismissing:

    func willDismissSearchController(_ searchController: UISearchController) {
        DispatchQueue.main.async {
            searchController.transitionCoordinator?.animate(alongsideTransition: { (context) in
                // animate views
            }, completion: nil)
        }
    }
    

    This works for me from iOS 9

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