How to animate alongside a UISearchController presentation/dismissal animation?

断了今生、忘了曾经 提交于 2019-12-01 03:22:22

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

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